File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array assignment

 
sakthi vadivel
Ranch Hand
Posts: 44
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how this is true int [] [] arr=new int[3] [];

why this is error int [] [] arr=new int[] [3];
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
-------------------------------------------
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic