Java Reference
In-Depth Information
String[] dangers = new String[]{"Lions", "Tigers", "Bears"};
This form of array creation expression allows you to create and initialize
an array anywhere. For example, you can create and initialize an array
when you invoke a method:
printStrings(new String[] { "one", "two", "many" });
An unnamed array created with new in this way is called an anonymous
array.
The last value in the initializer list is also allowed to have a comma after
it. This is a convenience for multiline initializers so you can reorder, add,
or remove values, without having to remember to add a comma to the
old last line, or remove it from the new last line.
Arrays of arrays can be initialized by nesting array initializers. Here is a
declaration that initializes an array to the top few rows of Pascal's tri-
angle, with each row represented by its own array:
int[][] pascalsTriangle = {
{ 1 },
{ 1, 1 },
{ 1, 2, 1 },
{ 1, 3, 3, 1 },
{ 1, 4, 6, 4, 1 },
};
Indices in an array of arrays work from the outermost inward. For ex-
ample, in the above array, pascalsTriangle[0] refers to the int array that
has one element, pascalsTriangle[1] refers to the int array that has two
elements, and so forth.
 
Search WWH ::




Custom Search