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.
Can't .... do .... plaid .... So I did this tiny ad instead:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database

This thread has been viewed 589 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 18, 2019 07:56:04.