ArrayLists and Iterators
6. Example Program Continued
Answer:
The list will hold references to Integer
objects.
Example Program Continued
|
In the program, the ArrayList
holds references of type String
or a subclass of String
.
In the picture, three elements have been added. The references are added in order starting with cell 0 of the array.
Then the program accesses the references using the get()
method and prints out the String
s. The output of the program is:
element 0: Amy
element 1: Bob
element 2: Cindy
The names.get( int index )
method returns the reference in the cell at index
. If the cell is empty, an IndexOutOfBoundsException
will be thrown and the program will halt (unless
there is code to handle the exception.)
Question 6:
Suppose that the following follows the first three
add
statements:names.add("Daniel");
Where will the reference to "Daniel" be added?