This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Whizlab mock : array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Whizlab mock : array" Watch "Whizlab mock : array" New topic
Author

Whizlab mock : array

adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186

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

Joined: Oct 20, 2007
Posts: 513
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.


SCJP 5.0
adam Lui
Ranch Hand

Joined: Sep 03, 2007
Posts: 186
thanks!
 
 
subject: Whizlab mock : array