View RSS Feed

Java Array

Array Initializers

Rate this Entry
by , 03-04-2012 at 10:14 AM (620 Views)
In a declaration, an array initialize might be specified. It also might be specified as an array creation expression part, that gives certain initial values and creates an array.

ArrayInitializer:
{ VariableInitializersopt ,opt }

VariableInitializers:
VariableInitializer
VariableInitializers , VariableInitializer

To make it further clear:

VariableInitializer:
Expression
ArrayInitializer

For writing array initialize, list of expressions are separated by commas and enclosed by "{" and "}".
The constructed array length would be equal to the no. of expressions.

An array initializerís expressions get executed starting from left towards right, in textual order which occurs in source code. Value of n-1st array component is specified by the nth variable initializer. With component type of an array, every expression shall be assignment compatible. In case component type is array type itself then array initializers might be nested. After array initializerís last expression, a trailing comma might come which is ignored.

Java Code:
For example: 
class Test {
	public static void main(String[] args) {
		int ia[][] = { {1, 2}, null };
		for (int i = 0; i < 2; i++)
			for (int j = 0; j < 2; j++)
				System.out.println(ia[i][j]);
	}
}
prints:
1
2

Submit "Array Initializers" to Facebook Submit "Array Initializers" to Digg Submit "Array Initializers" to del.icio.us Submit "Array Initializers" to StumbleUpon Submit "Array Initializers" to Google

Categories
Initialize

Comments