wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes array assignment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "array assignment" Watch "array assignment" New topic
Author

array assignment

sakthi vadivel
Ranch Hand

Joined: Dec 16, 2007
Posts: 41
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: array assignment