O'Reilly logo

The Java® Language Specification, Java SE 7 Edition, Fourth Edition by Alex Buckley, Gilad Bracha, Guy Steele, Bill Joy, James Gosling

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

10.4. Array Access

A component of an array is accessed by an array access expression (§15.13) that consists of an expression whose value is an array reference followed by an indexing expression enclosed by [ and ], as in A[i].

All arrays are 0-origin. An array with length n can be indexed by the integers 0 to n-1.

Example 10.4-1. Array Access

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 e : ia) sum += e;        System.out.println(sum);    }}

This program produces the output:

5050

The program declares a variable ia that has type array of int, that is, int[]. The variable ia is initialized to reference a newly created ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required