Java Reference
In-Depth Information
ArrayList<E>()
Constructor: creates an initially empty list.
boolean add (E obj)
Inserts the specified object to the end of this list.
void add ( int index, E obj)
Inserts the specified object into this list at the specified index.
void clear()
Removes all elements from this list.
E remove ( int index)
Removes the element at the specified index in this list and returns it.
E get ( int index)
Returns the object at the specified index in this list without removing it.
int indexOf (Object obj)
Returns the index of the first occurrence of the specified object.
boolean contains (Object obj)
Returns true if this list contains the specified object.
boolean isEmpty()
Returns true if this list contains no elements.
int size()
Returns the number of elements in this list.
FIGURE 5.8
Some methods of the ArrayList<E> class.
The program shown in Listing 5.11 instantiates an ArrayList<String> called
band . The method add is used to add several String objects to the end of the
ArrayList in a specific order. Then one particular string is deleted and another
is inserted at a particular index. As with any other object, the toString method
of the ArrayList class is automatically called whenever it is sent to the println
method, which prints all of the elements surrounded by square brackets. The while
loop at the end of the program explicitly prints each element on a separate line.
LISTING 5.11
//********************************************************************
// Beatles.java Author: Lewis/Loftus
//
// Demonstrates the use of a ArrayList object.
//********************************************************************
Search WWH ::




Custom Search