Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array declaration

 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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].
 
Jay Shukla
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent Rob !!!

I'm completely clear now. Thanks a Lot!!!
 
Clay Chow
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your explanations; both of you!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic