Java中如何初始化多维数组

83

如何正确声明一个多维数组并给它赋值?

这是我的代码:

int x = 5;
int y = 5;

String[][] myStringArray = new String [x][y];

myStringArray[0][x] = "a string";
myStringArray[0][y] = "another string";
7个回答

108

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]]]"

有用的文章


3
那不是一个“真正的”多维数组吗? - dhardy
13
“真正的”多维数组是指“非嵌套”的数组。有关嵌套数组和“真正的”多维数组之间的区别,请参见此问题 - aioobe
2
一个 int[i][j][k] 的长度为 i,因此它实际上等同于一个 ((int[k])[j])[i]。在声明中,大小与类型相反地书写,我想是为了使多维数组更类似于数学中的矩阵。 - Milosz
感谢提到Arrays.deepToString。 - samir105

64

尝试用以下内容替换相关行:

myStringArray[0][x-1] = "a string";
myStringArray[0][y-1] = "another string";

你的代码有误,因为子数组的长度为y,且索引从0开始。因此,设置myStringArray [0] [y]myStringArray [0] [x]将失败,因为索引xy超出了范围。

String [] [] myStringArray = new String [x] [y];是初始化矩形多维数组的正确方法。如果你想让它成为锯齿形(每个子数组可能具有不同的长度),那么你可以使用类似这个答案的代码。但请注意,在你想要一个完全矩形的多维数组的情况下,John声称必须手动创建子数组是错误的。


60

您还可以使用以下结构:

String[][] myStringArray = new String [][] { { "X0", "Y0"},
                                             { "X1", "Y1"},
                                             { "X2", "Y2"},
                                             { "X3", "Y3"},
                                             { "X4", "Y4"} };

13

你可以这样声明多维数组:

// 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"}};

9

Java中的多维数组

返回一个多维数组

Java并不真正地支持多维数组。在Java中,二维数组只是一个数组的数组,三维数组是一个数组的数组的数组,四维数组是一个数组的数组的数组的数组,以此类推...

我们可以定义一个二维数组:

  1. int[ ] num[ ] = {{1,2}, {1,2}, {1,2}, {1,2}}

  2. int[ ][ ] num = new int[4][2]

    num[0][0] = 1;
    num[0][1] = 2;
    num[1][0] = 1;
    num[1][1] = 2;
    num[2][0] = 1;
    num[2][1] = 2;
    num[3][0] = 1;
    num[3][1] = 2;
    

    If you don't allocate, let's say num[2][1], it is not initialized and then it is automatically allocated 0, that is, automatically num[2][1] = 0;

  3. Below, num1.length gives you rows.

  4. While num1[0].length gives you the number of elements related to num1[0]. Here num1[0] has related arrays num1[0][0] and num[0][1] only.

  5. Here we used a for loop which helps us to calculate num1[i].length. Here i is incremented through a loop.

    class array
    {
        static int[][] add(int[][] num1,int[][] num2)
        {
            int[][] temp = new int[num1.length][num1[0].length];
            for(int i = 0; i<temp.length; i++)
            {
                for(int j = 0; j<temp[i].length; j++)
                {
                    temp[i][j] = num1[i][j]+num2[i][j];
                }
            }
            return temp;
        }
    
        public static void main(String args[])
        {
            /* We can define a two-dimensional array as
                 1.  int[] num[] = {{1,2},{1,2},{1,2},{1,2}}
                 2.  int[][] num = new int[4][2]
                     num[0][0] = 1;
                     num[0][1] = 2;
                     num[1][0] = 1;
                     num[1][1] = 2;
                     num[2][0] = 1;
                     num[2][1] = 2;
                     num[3][0] = 1;
                     num[3][1] = 2;
    
                     If you don't allocate let's say num[2][1] is
                     not initialized, and then it is automatically
                     allocated 0, that is, automatically num[2][1] = 0;
                  3. Below num1.length gives you rows
                  4. While num1[0].length gives you number of elements
                     related to num1[0]. Here num1[0] has related arrays
                     num1[0][0] and num[0][1] only.
                  5. Here we used a 'for' loop which helps us to calculate
                     num1[i].length, and here i is incremented through a loop.
            */
            int num1[][] = {{1,2},{1,2},{1,2},{1,2}};
            int num2[][] = {{1,2},{1,2},{1,2},{1,2}};
    
            int num3[][] = add(num1,num2);
            for(int i = 0; i<num1.length; i++)
            {
                for(int j = 0; j<num1[j].length; j++)
                    System.out.println("num3[" + i + "][" + j + "]=" + num3[i][j]);
            }
        }
    }
    

4
我想补充一点,如果你想读取尺寸,你可以这样做:
int[][][] a = new int[4][3][2];

System.out.println(a.length);  // 4
System.out.println(a[0].length); // 3
System.out.println(a[0][0].length); //2

您还可以使用锯齿数组,其中不同的行具有不同的长度,因此a[0].length != a[1].length

1
 int[][] myNums = { {1, 2, 3, 4, 5, 6, 7}, {5, 6, 7, 8, 9, 10, 11} };
 for (int x = 0; x < myNums.length; ++x) {
    for(int y = 0; y < myNums[i].length; ++y) {
       System.out.print(myNums[x][y]);
    }
 }

输出

1 2 3 4 5 6 7 5 6 7 8 9 10 11


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接