CHAPTER 10: Arrays Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

10.5 A Simple Example

The example:


class Gauss {
	public static void main(String[] args) {
		int[] ia = new int[101];
		for (int i = 0; i < ia.length; i++)
			ia[i] = i;
		int sum = 0;
		for (int i = 0; i < ia.length; i++)
			sum += ia[i];
		System.out.println(sum);
	}
}

that produces output:

5050

declares a variable ia that has type array of int , that is, int[] . The variable ia is initialized to reference a newly created array object, created by an array creation expression (S15.9). The array creation expression specifies that the array should have 101 components. The length of the array is available using the field length , as shown.

The example program fills the array with the integers from 0 to 100 , sums these integers, and prints the result.

© 1996 Sun Microsystems, Inc. All rights reserved.