To declare a reference variable for an ArrayList, do this:
// myArrayList is a reference to a future ArrayList object
ArrayList myArrayList;
You do not say what type of object you are intending to store. An ArrayList
is like an array of references to Object
. This means that any object reference can be stored in an ArrayList
. To declare a variable and to construct an ArrayList
with an unspecified initial capacity do this:
// myArrayList is a reference to an ArrayList object. The
// Java system picks the initial capacity.
ArrayList myArrayList = new ArrayList();
This may not be very efficient. If you have an idea of what size ArrayList
you need, start your ArrayList
with that capacity. To declare a variable and to construct an ArrayList
with an initial capacity of 15, do this:
// myVector is a reference to an ArrayList object with an
// initial capacity of 15 elements.
ArrayList myArrayList = new ArrayList (15);
The elements of an ArrayList are accessed using an integer index. As with arrays, the index is an integer value that starts at 0.
- For retrieving data from an
ArrayList
the index is 0 to size-1
.
- For setting data in an
ArrayList
the index is 0 to size-1
.
- For inserting data into an
ArrayList
the index is 0 to size
. When you insert data at index size
, you are adding data to the end of the ArrayList
.
To add an element to the end of an ArrayList
use:
// add a reference to an Object to the end of the
// ArrayList, increasing its size by one
boolean add(Object obj);
Here is an example program. To use the ArrayList class you must import
the java.util
package:
Program 20 - 1
import java.util.*;
public class NameList
{
public static void main (String[] args)
{
ArrayList names = new ArrayList(10);
names.add("Cary");
names.add("Chris");
names.add("Sandy");
names.add("Elaine");
// remove the last element from the list
// note - the remove returns an object which must "cast" to
// a String before assignment. Explained in next section
String lastOne = (String)names.remove(names.size()-1);
System.out.println("removed: " + lastOne);
names.add(2, "Alyce"); // add a name at index 2
for (int j = 0; j < names.size(); j++)
System.out.println( j + ": " + names.get(j));
}
}
Run Output:
removed: Elaine
0: Cary
1: Chris
2: Alyce
3: Sandy
The add()
method adds to the end of an ArrayList
. To set the data at a particular index use:
// replaces the element at index with objectReference
Object set(int index, Object obj)
The index should be within 0
to size-1
. The data previously at index is replaced with obj
. The element previously at the specified position is returned.
To access the object at a particular index use:
// Returns the value of the element at index
Object get(int index)
The index should be 0
to size-1
.
Removing an element from a list: The ArrayList
class has a method that will do this without leaving a hole in place of the deleted element:
// Removes the element at index from thelist and returns
// its old value; decrements the indices of the subsequent
// elements by 1
Object remove(int index);
The element at location index
will be eliminated. Elements at locations index+1
, index+2
, ...
, size()-1
will each be moved down one to fill in the gap.
Inserting an element into an ArrayList
at a particular index: When an element is inserted at index the element previously at index is moved up to index+1
, and so on until the element previously at size()-1
is moved up to size()
. The size of the ArrayList
has now increased by one, and the capacity can be increased again if necessary.
// Inserts obj before the i-th element; increments the
// indices of the subsequent elements by 1
void add(int index, Object obj);
Inserting is different from setting an element. When set(index, obj)
is used, the object reference previously at index
is replaced by the new obj
. No other elements are affected, and the size does not change.