Java中多维数组的混淆

时间:2022-04-15 21:39:19

I'm not able to understand the following multi-dimensional code. Could someone please clarify me?

我无法理解以下多维代码。有人可以澄清一下吗?

int[][] myJaggedArr = new int [][] 
{
              new int[] {1,3,5,7,9},
              new int[] {0,2,4,6},
               new int[] {11,22}
   };

May I know how it is different from the following code?

我可以知道它与以下代码有何不同?

int[][] myArr = new int [][] {
             {1,3,5,7,9},
               {0,2,4,6},
                {11,22} };

6 个解决方案

#1


7  

It's not different at all. The former just makes it more explicit that you're creating an array of arrays.

它完全没有什么不同。前者只是让你更明确地创建一个数组数组。

#2


3  

No real difference. Just the first is declaring the sub arrays while the second is just placing values that are arrays into the array

没有真正的区别。第一个是声明子数组,而第二个只是将数组的值放入数组中

#3


2  

The two pieces of code produce identical results.

这两段代码产生相同的结果。

Multidimensional arrays are arrays of arrays.

多维数组是数组的数组。

  • myArr[0][1] would return 3
  • myArr [0] [1]将返回3
  • myArr[1][1] would return 2
  • myArr [1] [1]将返回2
  • myArr[2][0] would return 11
  • myArr [2] [0]将返回11

#4


0  

Completely the same - if you replace one with the other and compile, you will get byte-for-byte identical class files. Try it yourself!

完全相同 - 如果你用另一个替换并编译,你将得到逐字节的相同类文件。亲自尝试一下!

#5


0  

Both of these code snippets will result in multidimensional arrays with equivalent values.

这两个代码片段都将导致具有等效值的多维数组。

The second snippet shows that the explicit use of new is not needed when using the array literal shortcut for the internal arrays. This is also true for the outer array. This code could further be simplified to:

第二个片段显示在使用内部数组的数组文字快捷方式时不需要显式使用new。对于外部阵列也是如此。此代码可以进一步简化为:

int[][] myArr = { {1,3,5,7,9}, {0,2,4,6}, {11,22} };

The Oracle Java Tutorial does not show any examples of mixing the use of new to declare an array with the literal array declaration.

Oracle Java教程没有显示混合使用new来声明数组和文字数组声明的任何示例。

#6


0  

Starting with the beginning of declaring arrays in the fashion above.

从以上述方式声明数组开始。

You can create an array by stating:

您可以通过声明:创建一个数组:

int [] arr = new int[3]; //(eq 1)

int [] arr = new int [3]; //(eq 1)

You can go one step further by declaring the values in the array with:

您可以通过声明数组中的值来更进一步:

int [] arr = {0,1,2}; //(eq 2)

int [] arr = {0,1,2}; //(eq 2)

If you know your values ahead of time, you do not have to create an instance of int[].

如果您提前知道自己的值,则不必创建int []的实例。

Now to your question. There is no difference between the two, as others have stated, except for a more clear picture of what it is you are doing. The equivalent of eq. 2 in a two dimensional array is:

现在回答你的问题。正如其他人所说的那样,两者之间没有区别,除了更清楚地了解你在做什么。相当于eq。二维数组中的2是:

int [][] arr = {{0,1,2},{3,4,5},{6,7,8}}; //(eq 3)

int [] [] arr = {{0,1,2},{3,4,5},{6,7,8}}; //(eq 3)

Notice how you do not need to declare "new int[][]" before you start entering in values. "{0,1,2}" is an array itself and so is everything else within the first "{" and last }" in eq 3. In fact, after you declare arr, you can call the array "{0,1,2}" from eq 3 by the following statement:

注意在开始输入值之前,如何不需要声明“new int [] []”。 “{0,1,2}”是一个数组本身,所以在eq 3中第一个“{”和last}中的所有其他内容。事实上,在声明arr之后,你可以调用数组“{0,1 ,2}“来自以下声明的eq 3:

arr[0]; //(eq 4)

ARR [0]; //(eq 4)

eq 4 is equivalent to eq 2. You can switch out the simple "{" with "new int [] {" or "new int [][] {". If you want to switch out one for the other, that is fine and the only real difference is weather or not it fits your style of coding.

eq 4相当于eq 2.您可以使用“new int [] {”或“new int [] [] {”切换出简单的“{”。如果你想换一个用于另一个,那很好,唯一真正的区别是天气与否,它符合你的编码风格。

For fun, here is an example of a 3 dimensional array in short hand syntax:

为了好玩,这里是一个简短语法的三维数组示例:

//This is a really long statement and I do not recommend using it this way

//这是一个非常长的陈述,我不建议这样使用它

int [][][] arr = {{{0,1,2},{3,4,5},{6,7,8}}, {{9,10,11},{12,13,14},{15,16,17}}, {{18,19,20},{21,22,23},{24,25,26}}};

int [] [] [] arr = {{{0,1,2},{3,4,5},{6,7,8}},{{9,10,11},{12,13, 14},{15,16,17}},{{18,19,20},{21,22,23},{24,25,26}}};

#1


7  

It's not different at all. The former just makes it more explicit that you're creating an array of arrays.

它完全没有什么不同。前者只是让你更明确地创建一个数组数组。

#2


3  

No real difference. Just the first is declaring the sub arrays while the second is just placing values that are arrays into the array

没有真正的区别。第一个是声明子数组,而第二个只是将数组的值放入数组中

#3


2  

The two pieces of code produce identical results.

这两段代码产生相同的结果。

Multidimensional arrays are arrays of arrays.

多维数组是数组的数组。

  • myArr[0][1] would return 3
  • myArr [0] [1]将返回3
  • myArr[1][1] would return 2
  • myArr [1] [1]将返回2
  • myArr[2][0] would return 11
  • myArr [2] [0]将返回11

#4


0  

Completely the same - if you replace one with the other and compile, you will get byte-for-byte identical class files. Try it yourself!

完全相同 - 如果你用另一个替换并编译,你将得到逐字节的相同类文件。亲自尝试一下!

#5


0  

Both of these code snippets will result in multidimensional arrays with equivalent values.

这两个代码片段都将导致具有等效值的多维数组。

The second snippet shows that the explicit use of new is not needed when using the array literal shortcut for the internal arrays. This is also true for the outer array. This code could further be simplified to:

第二个片段显示在使用内部数组的数组文字快捷方式时不需要显式使用new。对于外部阵列也是如此。此代码可以进一步简化为:

int[][] myArr = { {1,3,5,7,9}, {0,2,4,6}, {11,22} };

The Oracle Java Tutorial does not show any examples of mixing the use of new to declare an array with the literal array declaration.

Oracle Java教程没有显示混合使用new来声明数组和文字数组声明的任何示例。

#6


0  

Starting with the beginning of declaring arrays in the fashion above.

从以上述方式声明数组开始。

You can create an array by stating:

您可以通过声明:创建一个数组:

int [] arr = new int[3]; //(eq 1)

int [] arr = new int [3]; //(eq 1)

You can go one step further by declaring the values in the array with:

您可以通过声明数组中的值来更进一步:

int [] arr = {0,1,2}; //(eq 2)

int [] arr = {0,1,2}; //(eq 2)

If you know your values ahead of time, you do not have to create an instance of int[].

如果您提前知道自己的值,则不必创建int []的实例。

Now to your question. There is no difference between the two, as others have stated, except for a more clear picture of what it is you are doing. The equivalent of eq. 2 in a two dimensional array is:

现在回答你的问题。正如其他人所说的那样,两者之间没有区别,除了更清楚地了解你在做什么。相当于eq。二维数组中的2是:

int [][] arr = {{0,1,2},{3,4,5},{6,7,8}}; //(eq 3)

int [] [] arr = {{0,1,2},{3,4,5},{6,7,8}}; //(eq 3)

Notice how you do not need to declare "new int[][]" before you start entering in values. "{0,1,2}" is an array itself and so is everything else within the first "{" and last }" in eq 3. In fact, after you declare arr, you can call the array "{0,1,2}" from eq 3 by the following statement:

注意在开始输入值之前,如何不需要声明“new int [] []”。 “{0,1,2}”是一个数组本身,所以在eq 3中第一个“{”和last}中的所有其他内容。事实上,在声明arr之后,你可以调用数组“{0,1 ,2}“来自以下声明的eq 3:

arr[0]; //(eq 4)

ARR [0]; //(eq 4)

eq 4 is equivalent to eq 2. You can switch out the simple "{" with "new int [] {" or "new int [][] {". If you want to switch out one for the other, that is fine and the only real difference is weather or not it fits your style of coding.

eq 4相当于eq 2.您可以使用“new int [] {”或“new int [] [] {”切换出简单的“{”。如果你想换一个用于另一个,那很好,唯一真正的区别是天气与否,它符合你的编码风格。

For fun, here is an example of a 3 dimensional array in short hand syntax:

为了好玩,这里是一个简短语法的三维数组示例:

//This is a really long statement and I do not recommend using it this way

//这是一个非常长的陈述,我不建议这样使用它

int [][][] arr = {{{0,1,2},{3,4,5},{6,7,8}}, {{9,10,11},{12,13,14},{15,16,17}}, {{18,19,20},{21,22,23},{24,25,26}}};

int [] [] [] arr = {{{0,1,2},{3,4,5},{6,7,8}},{{9,10,11},{12,13, 14},{15,16,17}},{{18,19,20},{21,22,23},{24,25,26}}};