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 ...

Get The Java® Language Specification, Java SE 7 Edition, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.