• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

multidimensional array

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]);
}
}
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It does seem a bit surprising. Apparently the new[] is auto generated whenever you leave it out so you can include it or not.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.???
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic