Modified from Lewis and Loftus, CS110 text, from chapter on Arrays (Chap 7)
The ArrayList class is part of the java.util package of the JDK (Java standard class library). It provides a service similar to an array in that it can store a list of values and reference them by index. However, whereas an array remains a fixed size throughout its existence, an ArrayList object dynamically grows and shrinks as needed. A data element can be inserted into or removed from any location (index) of an ArrayList object with a single method call. Unlike arrays, you can’t directly use an ArrayList to hold elements of a primitive type, like int []. ArrayLists can only hold elements of object type.
In its normal use, we define an ArrayList to accept a particular object type. That is, when we create the ArrayList object with “new”, we specify that it will be used to hold a certain class of objects. The following line of code creates an ArrayList object that stores String objects. Then one that stores Point objects.
ArrayList<String> names = new ArrayList<String>(); vs.:
String[] rainbow = new String[N];
ArrayList<Point> charges = new ArrayList<Point>(); vs.:
Point[] points = new Point[N];
Because of the <Type> flexibility, we say ArrayList is a “generic type”. The JDK has many other “collection classes” that have similar type flexibility. These are covered in Chap 11 and CS210.. CS110 only covers ArrayList<T>, where T is any object type.
First example: The Beatles started out with Paul, Pete, John, and George. Then Pete was replaced by Ringo.
We could do this with an array of String if the replacement was done in one action. But in between the loss of Pete and the addition of Ringo, the array would have a hard time representing the band of three, because it still has exactly 4 spots. We could replace Pete with a null String reference, indicating a vacancy. But with ArrayList, the hole is closed up nicely, and the structure has three elements, numbered 0, 1, 2, in the meantime.
//********************************************************************
// Beatles.java Author: Lewis/Loftus, modified by Betty O’Neil
//
// Demonstrates the use of an ArrayList object.
//********************************************************************
import java.util.ArrayList; <---Needed import
public class Beatles
{
//-----------------------------------------------------------------
// Stores and modifies a list of band members.
//-----------------------------------------------------------------
//
public static void main (String[] args)
{
ArrayList<String> band = new ArrayList<String>();
// form the original band
band.add ("Paul");
band.add ("Pete");
band.add (“John”);
band.add ("George");
System.out.println (band);
System.out.println("Find and remove Pete. Reprint band.");
int location = band.indexOf ("Pete");
band.remove (location);
System.out.println (band);
System.out.println("Find and print index 1 (was Pete).");
System.out.println ("At index 1: " + band.get(1));
System.out.println("Add Ringo at index 2");
band.add (2, "Ringo");
System.out.println("Print the band and the size of the band.");
System.out.println (band);
System.out.println ("Size of the band: " + band.size());
}
}
Output: This shows that ArrayList has a decent
toString that prints out the elements:
java
Beatles
[Paul, Pete, John, George]
<---useful
output of band.toString()
Find and remove Pete. Reprint band.
[Paul, John, George]
Find and print index 1 (was Pete).
At index 1: John
Add Ringo at index 2
Print the band and the size of the band.
[Paul, John, Ringo, George]
Size of the band: 4
Here
we are using ArrayList methods add, indexOf, remove, add (at position), and
size.
ArrayList API: not complete,
but includes all the methods listed on pg. 1114 of our text, plus isEmpty(),
also in common use.
T
can be any object type. The list of
calls on pg. 1114 is not really an API, though, because it is not in the form
of method headers, but more like the calls we make to ArrayList in programs
public
class ArrayList<T>
ArrayList<T>() creates an
initially empty list.
boolean add(T obj)
inserts the
specified object to the end of this list. Always returns true.
void add (int
index, T obj) inserts
the specified object into this list at the specified index.
void clear()
removes all
elements from the list.
boolean contains(Object x)
returns
true if this list contains x (by equals of elements)
T get(int
index) returns the object at the specified
index, without removing it.
int indexOf(Object
x) returns
the index of the first occurrence of x (by equals of elements)
int lastIndexOf(Object
x)returns
the index of the last occurrence of x (by equals of elements)
T remove(int
index) removes
the element at the specified index in this list and returns it.
T set(int index, T obj) replaces the
element at the specific index, with obj (no change to size) .
int size()
returns the
number of elements in this list.
boolean isEmpty()
returns true if
this list contains no elements