This site is retired and is no longer updated since November 2021. Please visit our new website at https://www.teamscode.org for up-to-date information!
Lists and ArrayLists

Lists are an alternative way of storing information in respect to arrays. The List interface has many benefits which can make it preferable to arrays. Here are some of its features:

  1. Lists do not have a fixed size, and shrink and grow to meet the number of elements

  2. The last element’s index will always be n-1

  3. Lists can easily switch, add, and remove elements

List is an interface, and therefore needs to be initialized as one of List’s subclasses. ArrayList is a commonly used subclass of the List. Here is the implementation syntax:

    List<E> NAME = new ArrayList<E>()

Besides being an interface, List also has an unusual attribute known as a generic, . To be simple, generics allow an undefined class type variable to be used in the class or interface. In this situation, the generic refers to the type of value which is stored in the list.

Here is an example of ArrayList being used:

    import java.util.List;

    import java.util.ArrayList;

    // Put import statements at top of file

    List<Integer> a = new ArrayList<Integer>();   

    List<Integer> b = new ArrayList<>();

Both lists, a and b, store values of type Integer. When initializing the two, repeating the contents disclosed in the second generic is not required.

ArrayList Methods
.get(int index) Returns element at the specified index
.size() Returns number of elements in the arraylist
.add(E element) Adds element to the end of the arraylist
.add(int index, E element) Adds element to the specified index in the arraylist
.indexOf(E element) Returns the index of first occurance of the specified element
.isEmpty() Returns True if the arraylist is empty
.remove(int index) Removes the element at the specified index
.set(int index, E element) replaces element at index with E Also Returns replaced value

Here is an example of a method which uses List and ArrayList:

    public void Example() {

        List<Integer> l = new ArrayList<>();

        l.add(3);               // l = {3}

        l.add(2);               // l = {3, 2}

        l.get(0);               // Returns 3, the value at index 0

        l.get(l.indexOf(3));    // Returns 3

        l.add(1, 1);            // l = {3, 1, 2} Adds the value 1 at index 1

        l.set(l.indexOf(2), 4); // l = {3, 1, 4} 

        l.remove(l.indexOf(3)); // l = {1, 4} Remove the value 3 from the List

        l.remove(l.size() - 1); // l = {1} Remove last element from the List

        l.remove(0);            // l = {}

        l.isEmpty();            // Returns true

    }

A major difference between arrays and Lists is that ArrayLists and Lists can only store classes and not primitives. In order to store primitives, wrapper classes are used to convert the primitives into objects in the process of boxing/wrapping. Some common wrapper classes are Integer and Double which both act as ints and doubles within an object form.

Luckily, with ArrayLists and many other similar classes, these primitives are wrapped to the correct type automatically in a process called auto-boxing when stored, and unboxing when being retrieved. However, it is important to understand manual boxing. Here are some examples:

    int a = 3

    Integer i = new Integer(a);             // boxing

    Integer i2 = a;                         // also valid

    i.intValue();                           // unboxing

    double b = 3.0;

    Double d = new Double(b);               // boxing

    Double d2 = b;                          // also valid

    d.doubleValue();                        // unboxing

    // makes object i with int value 3

    // returns int value 3

Overall, Lists are an essential part of programming that you must know well if you want to pursue a career in computer science. Lists, along with other classes/interfaces such as sets, maps, stacks, and queues, are important data structures that make up Java’s Collection interface. Once you finish the AP Computer Science course material, it is recommended that you explore these data structures next.

Lesson Quiz

1. Which is an incorrect creation of an ArrayList object?

a. ArrayList<Integer> a = new ArrayList<Integer>();
b. ArrayList<> a = new ArrayList<Integer>();
c. List<Integer> a = new ArrayList<>();
d. List<Integer> a = new ArrayList<Integer>();

2. What is the purpose of a wrapper class?

a. Wrapper classes allow classes to have a parameter of an undeclared type.
b. Wrapper classes act as primitives with methods which perform mathematical operations.
c. Wrapper classes are basic classes which are used manipulate primitive values.
d. Wrapper classes allow primitive values to be manipulated in an object format.

Written by James Richardson

Notice any mistakes? Please email us at learn@teamscode.com so that we can fix any inaccuracies.