Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Syntax for Array Initializer

 
Gino Fortunato
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Sergej Smoljanov
Ranch Hand
Posts: 467
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also you can populate each element individual or in for loop

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic