Initialize & loop one, two, three dimensional array in java (example)

  1. Given single or multi dimensional array in java.
  2. Initialize single or multi (two/three) dimensional array in java.
  3. Iterate or loop single or multi-dimensional array in java (with example)

1. Initialize Single/Multi-dimensional array in java

1.1) Static initialization of single/multidimensional array in java

  1. One dimension array
  2. Two dimension array
  3. Three dimension array

1.2.) Dynamic initialization of single/multidimensional array in java

  1. One dimension array
  2. Two dimension array
  3. Three dimension array

2. Loop/Iterate single & multi-dimensional array in java

  1. Print One dimension array
  2. Print Two dimension array
  3. Print Three dimension array

3. Code – Initialize & print multi dimensional array in java

import java.util.Arrays;
 
public class PrintMultiDimentionArray {
 
    public static void main(String[] args) {
        //Static initialization
        System.out.println("1. ******Static initialization of arrays*******");
        int staticOneDimension[] = {1,2,3,4,5};
         
        System.out.println("One Dimension array : "+ Arrays.toString(staticOneDimension));
        int staticTwoDimension[][] = {
                                        {10,20},
                                        {100,200},
                                        {1000,2000}
                                     };
        System.out.println("Two Dimension array : "+Arrays.deepToString(staticTwoDimension));
         
        int staticThreeDimension[][][] = {
                                            {
                                                {10,20,30},
                                            },
                                            {
                                                {100,200,300},
                                            },
                                            {
                                                {1000,2000,3000}
                                            }
                                        };
        System.out.println("Three Dimension array : "+Arrays.deepToString(staticThreeDimension));
         
        System.out.println("2. ******Dynamic initialization of arrays*******");
        int oneDimension[] = new int[4];
        for(int index = 0 ; index < oneDimension.length; index++)
            oneDimension[index] = index * index;
         
        System.out.println("One Dimension array : "+ Arrays.toString(oneDimension));
        int matrix[][] = new int [3][3];
        for (int row = 0 ; row < matrix.length; row++)
            for(int col = 0 ; col < matrix.length; col++)
                matrix[row][col] = row * col;
         
        System.out.println("Two Dimension array : "+Arrays.deepToString(matrix));
         
        int xyzSpace[][][] = new int [2][3][2];
        for (int x = 0 ; x < xyzSpace.length; x++)
            for(int y = 0 ; y < xyzSpace.length; y++)
                for(int z = 0 ; z < xyzSpace.length; z++)
                    xyzSpace[x][y][z] = x * y * z;
         
        System.out.println("Three Dimension array : "+Arrays.deepToString(xyzSpace));
    }
}

4. Print one, two, three dimensional array output in java

1. ******Static initialization of arrays*******
One Dimension array : [1, 2, 3, 4, 5]
Two Dimension array : [[10, 20], [100, 200], [1000, 2000]]
Three Dimension array : [[[10, 20, 30]], [[100, 200, 300]], [[1000, 2000, 3000]]]
 
2. ******Dynamic initialization of arrays*******
One Dimension array : [0, 1, 4, 9]
Two Dimension array : [[0, 0, 0], [0, 1, 2], [0, 2, 4]]
Three Dimension array : [[[0, 0], [0, 0], [0, 0]], [[0, 0], [0, 1], [0, 0]]]