Java Chapter 12 - ArrayList

ArrayList is a Java class that provides an automatic resizable array.  Normal arrays are not dynamic and cannot be resized after they are created.  The ArrayList class includes methods to add, remove, and search for elements.  It can be implemented to do database and data structure functions.

12.1 ArrayList Methods

To use the ArrayList class, you must first import java.util.ArrayList, or you can import java.util.*

Below is an example of declaring an ArrayList of integers.  You must use the Integer wrapper class (chapter 6.2).

ArrayList<Integer> myNumbers = new ArrayList<Integer>();

To declare an ArrayList of Strings:

ArrayList<String> myColors = new ArrayList<String>();

Use the following methods to work with the ArrayList class:

ArrayList Methods
Method Example Description
add(element)
myColors.add("purple");
myColors.add("orange");
myColors.add("green");
Adds elements to the end of the ArrayList.  In this case, "purple" is index 0, and "green" is index 2.
add(index, element)
myColors.add(0, "pink");
Adds an element at index 0 (beginning of ArrayList).

size()

myColors.size();

Returns the number of elements in the ArrayList.

indexOf(element)

myColors.indexOf("green");

Searches the ArrayList for the element and returns its index.  If the element is not found, it will return -1.

get(index)
myColors.get(1);
Returns the element at index 1.
set(index, element)
myColors.set(1,"yellow");
Replaces item at index 1 with "yellow"
contains(element)
myColors.contains("red");
Returns true or false on whether the element was found in the ArrayList.
remove(index)
myColors.remove(2);
Removes element at index 2.
subList(from index, to index - 1)
myColors.subList(x,y);
Returns a sub list of myColors starting at index x and ending at index y-1.
removeRange(from index, to index - 1)
myColors.removeRange(x,y);
Removes the elements from x to y-1.

clear()

myColors.clear();

Removes all elements in the ArrayList.

 

12.2 Example ArrayList of Strings

The following is an example program using an ArrayList of Strings:

PeriodicTable.java
import java.util.*;
public class PeriodicTable
{
    public static void main (String[] args)
    {
        ArrayList<String> Elements = new ArrayList<String>();

        Elements.add("hydrogen");   // add elements to the ArrayList
        Elements.add("lithium");
        Elements.add("beryllium");
        Elements.add(1,"helium");   // add helium between hydrogen & lithium

        System.out.println("Size = " + Elements.size());
        System.out.println("Element 2 = " + Elements.get(2));
        System.out.println("Index of beryllium = " + Elements.indexOf("beryllium"));
        System.out.println("Contains carbon?" + Elements.contains("carbon"));
        System.out.println("All Elements: " + Elements);
    }
}
Size = 4
Element 2 = lithium
Index of beryllium = 3
Contains carbon? false
All Elements: [hydrogen, helium, lithium, beryllium]


12.3 Example ArrayList of Objects

The following is an example program using an ArrayList of objects (MonsterClass):
creepy.java
import java.util.*;
public class creepy
{
    static ArrayList<MonsterClass> Monsters = new ArrayList<MonsterClass>();

    public static void main (String[] args)
    {
        Monsters.add(new MonsterClass("Werewolf",100));
        Monsters.add(new MonsterClass("Vampire",150));
        Monsters.add(new MonsterClass("Sasquatch",200));

        System.out.println(Monsters.get(0).Species + "  HP = " + Monsters.get(0).HP);
        Monsters.get(0).Damage();
        System.out.println(Monsters.get(0).Species + "  HP = " + Monsters.get(0).HP);

        Monsters.remove(0);
    }
}

class MonsterClass
{
    public String Species;
    public int HP;

    MonsterClass (String NewSpecies, int NewHP)
    {
        Species = NewSpecies; HP = NewHP;
    }

    public void Damage()
    {
        HP = HP - 10;
    }
}
Werewolf HP = 100
Werewolf HP = 90