File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Head First Android this week in the Android forum!

# array assignment

Ranch Hand

Joined: Dec 16, 2007
Posts: 44
how this is true int [] [] arr=new int[3] [];

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

sakthi SCJP, SCWCD OCA
Prabhat Gupta
Ranch Hand

Joined: Jan 22, 2008
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.

Don't get me started about those stupid light bulbs.

subject: array assignment