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

array declaration

 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone explain why b gets a two dimensional declaration in this code:



Nevermind foo and bar; they are my examples to prove the point visually.
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because (note the comma separator)


is equivalent to


or

 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also following code is



equivalent to



and another way is


is equivalent to

 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few tips about array.....


The above code will declare two arrays of int primitive type.

This code will declare an int array named arr1 and another int variable named arr2.
so if you write something like this...


will probably declare a two dimensional array named b.
 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's becoming much clearer now; it's really about the placement of the [] in regards to the association of the variable.

Thanks all!
 
Shin Kudo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[] when appears behind the type applies to all variables in this declaration,
[] when appears behind a variable only applies to that particular variable.

You can see example from Arka Guhathakurta's post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic