Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# array assignment

Ranch Hand
Posts: 44
how this is true int [] [] arr=new int[3] [];

why this is error int [] [] arr=new int[] [3];

Prabhat Gupta
Ranch Hand
Posts: 135
-------------------------------------------
how this is true int [] [] arr=new int[3] [];

why this is error int [] [] arr=new int[] [3];
---------------------------------------------

Let us compara the intialization of 2-D array with 1-D array.
int [] num=new int[3];
it means num is an array reference that points to 3 integer variable.
int [] num={1,2,3}
it means num is an array reference that points to three integer which values are 1,2,3.

Here we notice that we must specify the number of reference it is pointing to.

Similarly for
int [] [] arr=new int[3] []; means arr is reference of two dimensional array
that ponits to 3 reference of 1-D array. also we can specify those 1-D array as arr[0]=new int[3]..arr[1]=new int[6]..arr[2]=new int[9]

as i mentioned earlier that we must provide the number of reference it would point.

but in the case of
int [] [] arr=new int[] [3]; we are not specifying that how many 1-D array will be pointed by 2-D array arr although we are specifying that each 1-D array reference will point to 3 int variable.

So,moral of story is we must specify the number of reference will be pointed by array reference variable.

if i m wrong any where please, correct me.