[Logo]
Forums Register Login
Whizlab mock : array

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?
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.
thanks!
It means our mission is in jeapordy! Quick, read this tiny ad!
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA


This thread has been viewed 542 times.

All times above are in ranch (not your local) time.
The current ranch time is
Aug 22, 2018 00:18:32.