Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whizlab mock : array

 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

there is an compile error at line 2,
whizlabs stated that -
"when we use this syntax to create arrays, we should not specify the size, the size is evaluated from the number of elements between the curly braces."

but isn't line 1 doing as such?
from any introduction of array tutorial, the most basic declaration would be
int[] table = new int[4]
not in curly braces?

does the annoynomous class has anything to do with it?
 
Kelvin Chenhao Lim
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not an anonymous class, but an anonymous array. The general syntax is:

new type[] { value1, value2, ..., valueN }

As you can probably tell, this syntax is just an extension of the normal array initializer syntax. Its advantage is that it allows you to express an array value without having to declare a local variable. It's particularly convenient for method parameters and return values, e.g.



As you found out, however, with this syntax you must not include the length of the array within the square brackets. The array length will be determined automatically from the number of elements within the curly braces.
 
adam Lui
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic