aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array initialisation doubt. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array initialisation doubt." Watch "Array initialisation doubt." New topic
Author

Array initialisation doubt.

Rajesh Shinde
Greenhorn

Joined: Jan 29, 2010
Posts: 11
Hi,
Can anyone please explain how does the below code compiles?

int[] i=new int[][]{{1}}[0];


scjp6 processing.....
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

It is like the following


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Rajesh Shinde wrote:Hi,
Can anyone please explain how does the below code compiles?

int[] i=new int[][]{{1}}[0];


Ah, yes. This is a good one.

To analyze:
1. on the left side of the equation you have a one-dimensional array.
2. on the right side of the equation you have an anonymous two-dimensional array
3. the equation can be broken down in these steps:
new int[][] // instantiates anonymous array
new int[][]{{1}} // initialize the anonymous array with one single element, "1", which automatically goes to position [0][0]
int[] i = new int[][]{{1}}[0] // obtains all the elements in row [0] of the anonymous array (which is just "1") and put into one-dimensional array i.

i array ends up getting "1" as the element at position 0.

It would be clearer (and much more fun) if you try this in the debugger mode on your IDE and watch the array variable, i.


SCJP 6
Rajesh Shinde
Greenhorn

Joined: Jan 29, 2010
Posts: 11
Got it..
Thanks Ankit,Larry.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array initialisation doubt.
 
Similar Threads
integer literals
Generating thumbnail on unix
getting values from JTable and putting them into textfields
What is the difference between these statements
can anyone please check my method