initialize 2 dimensional array java code example

Example 1: 2d array java

char[][] array = new int[rows][columns];

Example 2: two dimensional array in java example program

class MultidimensionalArray {
    public static void main(String[] args) {

        int[][] a = {
            {1, -2, 3}, 
            {-4, -5, 6, 9}, 
            {7}, 
        };
      
        for (int i = 0; i < a.length; ++i) {
            for(int j = 0; j < a[i].length; ++j) {
                System.out.println(a[i][j]);
            }
        }
    }
}

Example 3: declare matrix in java

int[][] multi = new int[5][];
multi[0] = new int[10];
multi[1] = new int[10];
multi[2] = new int[10];
multi[3] = new int[10];
multi[4] = new int[10];

Example 4: two dimensional array in java example program

int[][] a = new int[3][4];

Example 5: how to initialize one dimensional array in java

int[] a; // valid declaration
int b[]; // valid declaration 
int[] c; // valid declaration