在Java中初始化多维数组

bur*_*gar 78 java multidimensional-array

声明多维数组并为其赋值的正确方法是什么?

这就是我所拥有的:

int x = 5;
int y = 5;

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

myStringArray[0][x] = "a string";
myStringArray[0][y] = "another string";
Run Code Online (Sandbox Code Playgroud)

aio*_*obe 100

Java没有"真正的"多维数组.

例如,arr[i][j][k]相当于((arr[i])[j])[k].换句话说,arr它只是数组的阵列数组.

因此,如果你知道数组是如何工作的,你就知道多维数组是如何工作的!


宣言:

int[][][] threeDimArr = new int[4][5][6];
Run Code Online (Sandbox Code Playgroud)

或者,初始化:

int[][][] threeDimArr = { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } };
Run Code Online (Sandbox Code Playgroud)

访问:

int x = threeDimArr[1][0][1];
Run Code Online (Sandbox Code Playgroud)

要么

int[][] row = threeDimArr[1];
Run Code Online (Sandbox Code Playgroud)

字符串表示:

Arrays.deepToString(threeDimArr);
Run Code Online (Sandbox Code Playgroud)

产量

"[[[1, 2], [3, 4]], [[5, 6], [7, 8]]]"
Run Code Online (Sandbox Code Playgroud)

  • 对于"真正的"多维数组,我称之为"非锯齿状"数组.对于锯齿状数组和"真实"多维数组之间的区别,请参阅[this question](http://stackoverflow.com/questions/597720/what-is-differences-between-multidimensional-array-and-array-of-arrays -in-C). (13认同)
  • 那怎么不是一个"真正的"多维数组呢? (3认同)
  • `int [i] [j] [k]`的长度为`i`,所以它实际上相当于`((int [k])[j])[i]`.声明中的大小是针对类型向后写的,我想使多维数组更接近于数学中的矩阵. (2认同)

jam*_*les 63

尝试用以下代码替换相应的行:

myStringArray[0][x-1] = "a string";
myStringArray[0][y-1] = "another string";
Run Code Online (Sandbox Code Playgroud)

您的代码不正确,因为子数组的长度为y,并且索引从0开始.因此设置为myStringArray[0][y]myStringArray[0][x]将失败,因为索引xy超出范围.

String[][] myStringArray = new String [x][y];是初始化矩形多维数组的正确方法.如果你希望它是锯齿状的(每个子数组可能有不同的长度),那么你可以使用类似于这个答案的代码.但请注意,在您需要完美矩形多维数组的情况下,John必须手动创建子数组的断言是不正确的.


A_M*_*A_M 59

您还可以使用以下构造:

String[][] myStringArray = new String [][] { { "X0", "Y0"},
                                             { "X1", "Y1"},
                                             { "X2", "Y2"},
                                             { "X3", "Y3"},
                                             { "X4", "Y4"} };
Run Code Online (Sandbox Code Playgroud)

  • +1因为这实际上是我想要的. (5认同)

Cli*_*int 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"}};
Run Code Online (Sandbox Code Playgroud)


小智 8

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;
    
    Run Code Online (Sandbox Code Playgroud)

    如果你没有分配,比方说num[2][1],它没有初始化,然后自动分配0,即自动分配num[2][1] = 0;

  3. 下面,num1.length给你行.

  4. 同时num1[0].length为您提供与之相关的元素数量num1[0].这里num1[0]有相关的阵列num1[0][0]num[0][1]只.
  5. 在这里,我们使用了一个for循环来帮助我们计算num1[i].length.这i是通过循环递增的.

    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]);
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)