Getting the array length of a 2D array in Java

Consider public static void main(String[] args) { int[][] foo = new int[][] { new int[] { 1, 2, 3 }, new int[] { 1, 2, 3, 4}, }; System.out.println(foo.length); //2 System.out.println(foo[0].length); //3 System.out.println(foo[1].length); //4 } Column lengths differ per row. If you’re backing some data by a fixed size 2D array, then provide getters to … Read more

Create a two dimensional string array anArray[2][2]

Looks like this is what you want int columns = 2; int rows = 2; String[][] newArray = new String[columns][rows]; newArray[0][0] = “France”; newArray[0][1] = “Blue”; newArray[1][0] = “Ireland”; newArray[1][1] = “Green”; for(int i = 0; i < rows; i++){ for(int j = 0; j < columns; j++){ System.out.println(newArray[i][j]); } } Here I’ll explain the … Read more