jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes multidimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "multidimensional array" Watch "multidimensional array" New topic
Author

multidimensional array

Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
class C {
public static void main(String[] args) {
int[] a1[] = {new int[]{1,2},new int[]{3,4,5}};
int []a2[] = new int[][]{{1,2},{3,4,5}};
//why compiler accept this without any error.?
int a3[][] = {{1,2},new int[]{3,4,5}};
System.out.print(a1[0][1]+","+a2[1][0]+","+a3[1][2]);
}
}


Hard work beats talent<br />when talent doesn't work hard.<p> - Tim Notke
Greg Neef
Ranch Hand

Joined: Jun 16, 2003
Posts: 82
It does seem a bit surprising. Apparently the new[] is auto generated whenever you leave it out so you can include it or not.


SCJP 1.4
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
Originally posted by Harvinder Singh:
//why compiler accept this without any error.?
}

Which line are you refering to ?Is it the one above or below your comment?
Priyanka Chopda
Ranch Hand

Joined: Jul 22, 2003
Posts: 112
Harvinder,
------------------------------------------------
//why compiler accept this without any error.?
int a3[][] = {{1,2},new int[]{3,4,5}};
-------------------------------------------------
I'm assumin that you meant the above line.
Its just a different way of constructing an array as we do for 1-d array
i.e. int[] a4={2,3};
When we have 2-d array, its means arrays of arrays.
so with the above statement
a3[0][] is referring to array {1,2}
a3[1][]is referring to array {3,4,5}
hence when compiled it gives 2,3,5
i.e. a3[1][2]=5
-PC
Harvinder Singh
Ranch Hand

Joined: Feb 14, 2003
Posts: 90
There is one more problem that I would like to ask. I think the array a[3]
has the size 3.But in the Chisholm answers from where this code was taken the size of a3[][] is said to be 4.
Is that just a typing mistakes in the answers or I am wrong.???
Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
Harvinder Singh Ji
Sat Sri Akal!

------------------------------------------------
//why compiler accept this without any error.?
int a3[][] = {{1,2},new int[]{3,4,5}};
-------------------------------------------------

In the book by Khalid Moghul, Chapter 4, there is a concept of "Anonymous Arrays" which kind of explains the new int[]{3,4,5}}; form of array construction, which I guess is bothering you. Basically, this form returns a simple array but you don't have to name it or specify its type. It returns a reference which can be assigned to some appropriate array reference variable or passed as argument to a method etc.
And as for the second part of your question:
I think the array a[3]
has the size 3.But in the Chisholm answers from where this code was taken the size of a3[][] is said to be 4

Well I believe the size of a[3] should be 2, not 3, not 4. It has references to 2 arrays when it was constructed. So thats the length for it.
Cheers
Anupreet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multidimensional array