Forums Register Login

Syntax for Array Initializer

+Pie Number of slices to send: Send
in some of the certification mock tests, I see some very complicated looking syntax for the array iniitializer but i can't find any documentation for the more complicated syntax to declare and array of array and dimensions beyond that. Can someone point me to a source or explain it?

+Pie Number of slices to send: Send
You can find good explanation in books
OCA Java SE 7 Programmer I Certification Guide
or
OCA/OCP Java SE 7 Programmer I & II Study Guide (Exams 1Z0-803 & 1Z0-804)

is ok, but

you must use this variant if you use separate lines

int this case you must not do this new int[3] {1,2,3};
when you create multidimensional array you must at last one dimention to be:

in multidimensional array number of brackets (dimensional) must be same you cant do this

some poit to remember array is object, in 2d array each elemetn is array of 1d type:

where strA1 [0] - array of String
strA1 [1] - array of String

you can ever do this

str[0] - type is String [][] so you can do
str[0] = new String [0][1];
you can populate array with each element like up line and you can make this:
str[1] = new String [1][3];
str[2] = new String [1][8];


Some point to remember:
1.you can`t asign array of primitive one type to array of primitive other type.

2. For reference type you can asign array of sub type to array of super type:

3. Arrays of type Object, Serializable, Cloneable- can store arrays.

for Object [] element may be array of every type.
but you cant do this

4. some example of arrays and question here (array assignment and array access)
5. If you create array with negative index you will get NegativeArraySizeException.
6. If you try access array at position <0 or >=length (if array not point to null, and some other from array element access ) you will get ArrayIndexOutOfBoundsExeption



it is interesting example first i think it no compile but it is:

+Pie Number of slices to send: Send
also you can populate each element individual or in for loop

+Pie Number of slices to send: Send
 

Gino Fortunato wrote:I see some very complicated looking syntax for the array iniitializer


I can think only of these 3 forms to initialize an array:


No one looks very complicated to me, but that's probably because I use them for over 10 years now Maybe you are referring to the last one (a3)? Or maybe it's about multi-dimensional arrays? That syntax can look very complicated/overwhelming at first. If that's the case, you should have a look here.
eat bricks! HA! And here's another one! And a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 573 times.
Similar Threads
declare array in java
How to Set Array in Value 3
what is going on??
Passed the 1Z0-808 Java SE 8 Programmer I exam with 94% (my experience with in detail)
Int Array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:08:15.