开发者

Confusion in multi dimensional array in Java

开发者 https://www.devze.com 2023-01-02 09:00 出处:网络
I\'m not able to understand the following multi-dimensional code. Could someone please clarify me? int[][] myJaggedArr = new int [][]

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},
  开发者_StackOverflow社区            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[1][1] would return 2
  • myArr[2][0] would return 11


Completely the same - if you replace one with the other and compile, you will get byte-for-byte identical class files. 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. 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.


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[].

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:

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:

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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号