ArrayLists and Iterators
Read this chapter about lists and the useful methods that are available for adding and inserting elements into a list.
10. Summary List Interface
Answer:
0, just as with an array.
Summary List<E>
Interface
List<E>
Interface
Here is a summary of the List<E>
Interface. For the full interface see the Java documentation.
Method | What it Does |
---|---|
boolean add(E elt) |
Appends element elt to the end of the list. |
void add(int index, E elt) |
Inserts element elt at the specified index. |
E remove(int index) |
Removes the element at the specified index. |
void clear() |
Remove all elements, so the list is now empty. |
int indexOf(Object elt) |
Searches for the first occurrence of elt, testing for equality using the equals(Object) method. |
boolean isEmpty() |
tests if the list is empty |
E set(int index, E elt) |
Replaces the element at index with the specified element elt. |
. . . many more . . . | . . . |
The E
in these descriptions means reference to object of type E
or of its subclasses. Of course, the List
must have been constructed to hold references to that type.
Question 10:
Here is a declaration and construction of a
ArrayList
:ArrayList<String> data = new ArrayList<String>(10);
Which of the following statements will work?
data.add( "Irene Adler" );
data.add( new String("Laura Lyons") );
data.add( 221 );
data.add( new Integer( 221 ) );