class jarray { public static single void main(String[] args) { /* Doesn't work */ int[][] matrix = new int[4][4]; for (int y = 0; y < matrix.length; y++) { for (int x = 0; x < matrix[y].length; x++) { matrix[y][x] = 10*y + x; } } for (int y = 0; y < matrix.length; y++) { for (int x = 0; x < matrix[y].length; x++) { System.out.println(x + " " + y + " " + matrix[y][x]); } } /* Works */ int[][] matrix2 = new int[4][]; for (int y = 0; y < matrix2.length; y++) { matrix2[y] = new int[4]; } for (int y = 0; y < matrix2.length; y++) { for (int x = 0; x < matrix2[y].length; x++) { matrix2[y][x] = 10*y + x; } } for (int y = 0; y < matrix2.length; y++) { for (int x = 0; x < matrix2[y].length; x++) { System.out.println(x + " " + y + " " + matrix2[y][x]); } } } }