至尊宝的传说
Java没有“真”多维数组。例如,arr[i][j][k]等于((arr[i])[j])[k]..换句话说,arr是简单的数组、数组的数组.所以,如果您知道数组是如何工作的,那么您就知道多维数组是如何工作的!声明:int[][][] threeDimArr = new int[4][5][6];或者,在初始化时:int[][][] threeDimArr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };准入:int x = threeDimArr[1][0][1];或int[][] row = threeDimArr[1];字符串表示:Arrays.deepToString(threeDimArr);产量"[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]"
海绵宝宝撒
您可以声明多维数组如下:// 4 x 5 String arrays, all Strings are null// [0] -> [null,null,null,null,null]// [1] -> [null,null,null,null,null]// [2] -> [null,null,null,null,null]// [3] -> [null,null,null,null,null]String[][] sa1 = new String[4][5];for(int i = 0; i < sa1.length; i++) { // sa1.length == 4 for (int j = 0; j < sa1[i].length; j++) { //sa1[i].length == 5 sa1[i][j] = "new String value"; }}// 5 x 0 All String arrays are null// [null]// [null]// [null]// [null]// [null]String[][] sa2 = new String[5][];for(int i = 0; i < sa2.length; i++) { String[] anon = new String[ /* your number here */]; // or String[] anon = new String[]{"I'm", "a", "new", "array"}; sa2[i] = anon;}// [0] -> ["I'm","in","the", "0th", "array"]// [1] -> ["I'm", "in", "another"]String[][] sa3 = new String[][]{ {"I'm","in","the", "0th", "array"},{"I'm", "in", "another"}};