This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this valid?

 
K Ville
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Anupreet Arora
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Munish Gulati
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic