Java Reference
In-Depth Information
Consider the following statement:
int [] list = new int [10];
This statement declares an array list of 10 elements. The elements are
list[0] , list[1] ,..., list[9] . In other words, we have declared 10 variables of type
int (see Figure 9-3).
[0]
[1] [2]
0
[3] [4] [5]
0
[6]
0
[7]
[8]
[9]
list
0
0
0
0
0
0
0
0
0
0
FIGURE 9-3 Array list
The assignment statement:
list[5] = 34;
stores 34 into list[5] , which is the sixth element of the array list (see Figure 9-4).
[0]
[1] [2]
0
[3] [4] [5]
34
[6]
0
[7]
[8]
[9]
list
0
0
0
0
0
0
0
0
0
0
FIGURE 9-4 Array list after the execution of the statement list[5]= 34;
Suppose i is an int variable. Then, the assignment statement:
list[3] = 63;
is equivalent to the assignment statements:
i = 3;
list[i] = 63;
If i is 4 , then the assignment statement:
list[2 * i - 3] = 58;
stores 58 into list[5] , because 2 * i - 3 evaluates to 5 . The index expression is
evaluated first, giving the position of the element in the array.
Next, consider the following statements:
list[3] = 10;
list[6] = 35;
list[5] = list[3] + list[6];
 
Search WWH ::




Custom Search