Tino APCS

The ArrayList Class

To declare a reference variable for an ArrayList, do this:

// myArrayList is a reference to
// a future ArrayList object
ArrayList <ClassName> myArrayList;

An ArrayList is an array of references to objects of type ClassName, where ClassName can be any class defined by you or Java.

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 <ClassName> myArrayList = new ArrayList <ClassName> ();


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:

// myArrayList is a reference to an ArrayList
// object with an initial capacity of 15 elements.
ArrayList <ClassName> myArrayList = new ArrayList <ClassName> (15);


One way of accessing the elements of an ArrayList is by using an integer index. The index is an integer value that starts at 0 and goes to size()-1.

To access the object at a particular index, use:

// Returns the value of the element at index
Object get(int index);

System.out.println(myArrayList.get(i));


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);

Program Example 15 - 1:

import java.util.ArrayList;

ArrayList <String> names = new ArrayList <String> (10);

names.add("Cary");
names.add("Chris");
names.add("Sandy");
names.add("Elaine");

// remove the last element from the list

String lastOne = 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


A shorthand way to iterate through the collection is provided by a “for each” loop. A for each loop starts you at the beginning of the collection and proceeds through all of the elements. It will not allow you to skip elements, add elements or remove elements.

An example of using it on the collection created in the previous section is

for(String n : names){
    System.out.println(n);
}


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.

Removing an element from a list: The ArrayList class has a method that will remove an element from the list without leaving a hole in place of the deleted element:

// Removes the element at index from the list 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 position: 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.

Whether you are adding at the beginning, middle or end, remember that you are adding an object and must instantiate that object somewhere. Strings hide this fact.

names.add(2, "Alyce");


This statement actually creates a String object with the value Alyce.

If we are using any other object, we must instantiate the object. If we have an ArrayList drawList of DrawingTools, we could add a DrawingTool in the following way.

drawList.add(**new**  DrawingTool());


Dark Mode

Outline