Java Reference
In-Depth Information
The indexed variables of an array can be used as an argument to be plugged in for
a parameter of the array's base type.
A method can have parameters of an array type. When the method is invoked, an
entire array is plugged in for the array parameter.
A method may return an array as the value returned by the method.
When using a partially filled array, your program needs an additional variable of
type int to keep track of how much of the array is being used.
An instance variable of a class can be of an array type.
If you need an array with more than one index, you can use a multidimensional
array, which is actually an array of arrays.
Answers to Self-Test Exercises
1. a. word
b. String
c. 5
d. 0 through 4 inclusive
e. Any of the following would be correct:
word[0] , word[1] , word[2] , word[3] , word[4]
2. a. 10
b. 0
c. 9
3. a, b, c,
4. 1.1 2.2 3.3
1.1 3.3 3.3
5.
The for loop uses indices 1 through sampleArray.length , but the correct indices
are 0 through sampleArray.length 1 . The last index, sampleArray.length , is
out of bounds. What was probably intended is the following:
int [] sampleArray = new int [10];
for ( int index = 0; index < sampleArray.length; index++)
sampleArray[index] = 3*index;
6.
The last value of index is a.length 1 , which is the last index of the array. How-
ever, when index has the value a.length 1 , a[index + 1] has an index that is
out of bounds because index + 1 is one more than the largest array index. The
for loop ending condition should instead be index < a.length 1 .
Search WWH ::




Custom Search