This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Array declaration Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array declaration" Watch "Array declaration" New topic
Author

Array declaration

Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35
From the www.examlab.tk SCJP 1.5 Exam Simulator:

Why is the following a legal declaration ? Initially, i though it was legal because it was an array of two dimensional arrays. But, from playing around with the code, it appears to be a simple one dimensional array of ints.



int [] it2 = new int[][]{{1}}[0];
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The important thing to know is that in Java, a 2d array is just an array of 1d arrays. The expression

new int[][]{{1}}

is a two-dimensional array of integers; each dimension is 1 (i.e., it's a 1x1 array.) The single element value the value "1".

Now if you add [0] to to the end, that means the 0-th element of the first dimension of the array. A 2d int array is an array of 1d arrays of ints, so

new int[][]{{1}}[0]

is the first element of the 2d array -- which is a 1d array, one element long.

Make sense?


[Jess in Action][AskingGoodQuestions]
Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
Good Explanation Earnest!!!

But Waht does {{1}} indicates.

Also Can you explain whole meaining of new int[][]{{1}}[0]
in single sentence.

Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Originally posted by Jay Shukla:
But Waht does {{1}} indicates.

You can create arrays in the declaration using curly braces. You start with {, then you add all elements separated by commas, and you end with } (and ; to end the statement / declaration).

So {{1}} works as follows:
- { starts a new array
- { starts a new array in the existing array
- 1 is the sole element
- } ends the inner array
- } ends the outer array

I think you'll now be able to tell what the following means:
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}

Also Can you explain whole meaining of new int[][]{{1}}[0]
in single sentence.

Let's keep it to multiple sentences to keep a bit of an overview
- new int[][]: create a new array of arrays of ints.
- {{1}}: initialize the array you just created as I already explained earlier
- [0]: from the array you just created and initialized, take the first element.

So it creates an array. This array has one element, which is an array of ints. This array has one element, 1. This array is returned using [0].


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jay Shukla
Ranch Hand

Joined: Jun 08, 2008
Posts: 214
Excellent Rob !!!

I'm completely clear now. Thanks a Lot!!!
Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35
Thanks for all your explanations; both of you!
 
Consider Paul's rocket mass heater.
 
subject: Array declaration
 
Similar Threads
One more Confusing Output
Please hava a look at this question about array !!!
arrays declarations
multidimensional arrrays
MultiDimensional Arrays