• Post Reply Bookmark Topic Watch Topic
  • New Topic

array declaration  RSS feed

 
mike Vigor
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between


and
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No difference between these two statements. Other way round, they have something in common - bad and inconsistent style.

1. [] better would be written after the data type - 'int[]' (no space after int)
2. myarray better would read as myArray, in fact it isn't good name too, but at least follows naming convention
3. before and after operator '=' supposed to be an empty spaces, but if you were to move [] by the data type, that problem would have dissapeared itself
4. {1,3, 3, 4} if you noticed, after the '1' you don't have a space character while after the first '3' and second '3' you do have, so your formatting isn't consistent
5. missing semi colon at the end of statement

similar problems as in above statement, but better to use statement above to initialize an array.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now I have improved the style a bit:-That is the full form of an array initialiser, on the right of the = operator. If you declare and initialise the array on the same line, you are allowed to miss part out:-
int[] myArray = new int[]{1, 2, 3, 4};
int[] myArray = {1, 2, 3, 4};
If you are assigning anywhere other than at the time of declaration, you must include new int[]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you guys missed the most obvious. The first example creates an array where the 2nd and 3rd elements are the same. The second example creates an array where all four elements are distinct.
 
Paul Clements
Ranch Hand
Posts: 99
1
Chrome Eclipse IDE MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:you guys missed the most obvious. The first example creates an array where the 2nd and 3rd elements are the same. The second example creates an array where all four elements are distinct.

Ha ha...true. Looks like a case of assuming the data was the same based on the way the question was phrased.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!