简体   繁体   English

Java中多维数组的混淆

[英]Confusion in multi dimensional array in Java

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

It's not different at all. 它完全没有什么不同。 The former just makes it more explicit that you're creating an array of arrays. 前者只是让你更明确地创建一个数组数组。

No real difference. 没有真正的区别。 Just the first is declaring the sub arrays while the second is just placing values that are arrays into the array 第一个是声明子数组,而第二个只是将数组的值放入数组中

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

Completely the same - if you replace one with the other and compile, you will get byte-for-byte identical class files. 完全相同 - 如果你用另一个替换并编译,你将得到逐字节的相同class文件。 Try it yourself! 亲自尝试一下!

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. 第二个片段显示在使用内部数组的数组文字快捷方式时不需要显式使用new This is also true for the outer array. 对于外部阵列也是如此。 This code could further be simplified to: 此代码可以进一步简化为:

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来声明数组和文字数组声明的任何示例。

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)

You can go one step further by declaring the values in the array with: 您可以通过声明数组中的值来更进一步:

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. 相当于eq。 2 in a two dimensional array is: 二维数组中的2是:

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

arr[0]; //(eq 4)

eq 4 is equivalent to eq 2. You can switch out the simple "{" with "new int [] {" or "new int [][] {". eq 4相当于eq 2.你可以用“new int [] {”或“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. 如果你想换一个用于另一个,那很好,唯一真正的区别是天气与否,它符合你的编码风格。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM