Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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} };
like image 438
Alvin Avatar asked Jun 03 '10 17:06

Alvin


3 Answers

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

like image 172
sepp2k Avatar answered Nov 13 '22 02:11

sepp2k


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

like image 3
geshafer Avatar answered Nov 13 '22 00:11

geshafer


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
like image 2
Simon Avatar answered Nov 13 '22 00:11

Simon