• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is this valid?

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given:
int[] a1[],a2[];

It seems to me that the 1st array obj ref is a 2d array, and the 2nd one is a simple array.
 
K Ville
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got a follow-up from a mock I'm presently taking.
Given:
int[][] a = {{1,2,3},{4,5,6},{7,8,9,10}};
Is this also valid? Is it not that the 2nd pair of curly braces must be a set of parentheses?
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Given:
int[] a1[],a2[];
It seems to me that the 1st array obj ref is a 2d array, and the 2nd one is a simple array.


Well both of them are 2D arrays. What you say would have been true if the declaration had been sth like:
int[] a1[],a2;

Given:
int[][] a = {{1,2,3},{4,5,6},{7,8,9,10}};
Is this also valid? Is it not that the 2nd pair of curly braces must be a set of parentheses?


Yes, this is valid as is this how multidimesional arrays are declared and contructed, using curly braces and not parenthesis.
Regards,
Anupreet
 
K Ville
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so much! But do you also mean that:

int[] a1[], a2
is a valid declaration? IF so, I never knew this. It would be a great help from you. God bless!
 
Ranch Hand
Posts: 8945
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it is! a1 is a 2 D array and a2 is a 1-D array!

Originally posted by K Ville:
Thanks so much! But do you also mean that:

int[] a1[], a2
is a valid declaration? IF so, I never knew this. It would be a great help from you. God bless!


[ September 02, 2003: Message edited by: Pradeep Bhat ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check out this thread that just came up a couple weeks ago, it might help some more:
2 D Arrays
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And one more thing about the above discussion...now as you know
int[] a1[], a2; is a valid declaration so assignments
a1=a2;
or
a2=a1;
will result in a compiler error.
while in case int[] a1[],a2[] both a1 and a2 are 2D arrays so
a1=a2;
or
a2=a1;
are valid.
[ September 02, 2003: Message edited by: Thomas Paul ]
[ September 02, 2003: Message edited by: Karan Gulati ]
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic