Page 2 of 2
There is an easy-to-use shorthand for initializing array references:
String [][] saa = {
{ { "Hello, World }, { "Guten Tag, Welt"} } };
// this creates a String[][] object like the one created
// above, and assigns saa to refer to that object.
// The whitespace is meant to emphasize that the
// object created is an array of one String[] which
// contains two Strings.
Using this shorthand, our example could correctly be written as:
String [][] saa = { { { "Help" } } };
However, this makes saa refer to a one by one string array. Note that the syntax above only works when initializing an array reference (initialization
is the special case of assignment at the time of declaration). The more general way to create a new array and assign it to
a new or existing array reference looks like this (in the case of an existing reference):
saa = new String [][] {
// note the empty [][] -- the compiler figures the
// size out (empty [][] is required).
{ { "Hello" }, { "World" } } // this is saa[0]
, // note the comma separating
saa[0] and saa[1]
{ { "Guten Tag" }, { "Welt"
} } // this is saa[1]
};
// now saa.length = 2, and saa[0] and saa[1] also each have length 2
use rubyBy Anonymous on July 23, 2009, 11:09 amuse ruby
Reply | Read entire comment
Array + arrayBy Anonymous on July 19, 2009, 6:31 amhow can i do {1,2,3,4} + {5,6} so i get {1,2,3,4,5,6}?
Reply | Read entire comment
View all comments