Please enable JavaScript to use CodeHS

Chapter 7

ArrayLists

7.2 ArrayList Methods

ArrayLists provide flexibility and functionality that arrays do not. You can think of an ArrayList as a container that will resize as you add and remove objects from it. In order to do so, there are several helper methods that are used to alter the state of an ArrayList, and get information about its content.

Adding Elements to an ArrayList

Objects can be added to an ArrayList using the add method. The add method has several method signatures:

boolean add(E obj)
void add(int index, E obj)
Plain text

The single parameter add method will add an element to the end of the ArrayList:

import java.util.ArrayList;

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5);
Plain text

Since the ArrayList starts with no values, the call to add will add the element 5 to the 0th index of the ArrayList. If the call list.add(8) were added to this code segment, the element 8 would be added to the 1st index position. Like arrays, ArrayLists begin at index 0. The add method returns true if the element was successfully added to the ArrayList.

The second add method signature will add an element at a specified index:

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(1, 4);  //Adds the value 4 at index 1.
Plain text

The specified index must be between the value 0, and the current size of the ArrayList. To determine the size of the ArrayList, the method size can be used to return the current length of an ArrayList:

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5);
System.out.println(list.size()); //returns 1
list.add(1, 4);
System.out.println(list.size()); //returns 2
Plain text

Notice that the size of the ArrayList increases when a new value is added to the list. When add(int index, E obj) is called, the list items from index - size() shift up one index to allow the new value to slot in to the correct index position.

Getting Elements in an ArrayList

Elements in an ArrayList can be accessed using the get(int index) method:

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(3);
list.add(10);
list.add(4);

int val = list.get(1);
System.out.println(val); //prints the value 3
Plain text

The method get returns an element at the specified index. In this case, the value stores at index 1 (3), is being stored in the variable val, and printed to the console.

Setting Elements in an ArrayList

Individual elements in an ArrayList can be changed using the set(int index, E obj) method:

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(3);
list.add(10);
list.add(4);

int val = list.get(2);
System.out.println(val); //prints the value 10

//change the value at index 2
list.set(2, 34);
val = list.get(2);
System.out.println(val); //prints the value 34
Plain text

In this example, the value at index 2 is changed from the value 10 to the value 34. It’s important to note that the values can only be changed to another value of the same data type.

The set method also returns the value that is being replaced to the existing program. The value that has been replaced can then be stored in a variable of its own:

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(3);
list.add(10);
list.add(4);

int val = list.set(2, 34);
System.out.println(val); //prints the value 10

int newVal = list.get(2);
System.out.println(newVal); //prints the value 34
Plain text

Removing Elements from an ArrayList

Just as elements can be added, they can be removed from an ArrayList using the remove(int index) method. Like the set method, the remove method also returns the value that is being removed from the specified index.

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(5);
list.add(3);
list.add(10);
list.add(4);
System.out.println(list.size()); // prints the value 4

int val = list.remove(0);
System.out.println(val); //prints the value 5

System.out.println(list.size()); // prints the value 3
Plain text

In the program above, notice that the size of the ArrayList decreases by one when the element is removed from the ArrayList. All values at the index higher will move down one index once an element is removed from an ArrayList.

Comparing Array and ArrayList Functionality

Many of the methods used to access and manipulate ArrayLists are functionally similar in nature to arrays:

Feature Arrays ArrayLists
Initialize int[] arr = new int[5]; ArrayList array = new ArrayList();
Get Value arr[index] array.get(index)
Get Size arr.length array.size()
Set Value arr[index] = value; array.set(index, value)

ArrayLists Differ In Their Ability to Remove or Add Values

Feature Arrays ArrayLists
Remove Value N/A array.remove(index)
Add Value N/A array.add(value) OR array.add(index, value)

When considering which data structure to use, it’s important to determine early on if it’s necessary to add or remove values at future points in the program. If that’s the case, then ArrayLists may serve better.

ArrayList and Java Primitives

ArrayList Methods

Array vs. ArrayList Methods

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    What value will be printed in the console after this series of commands? Assume that the ArrayList package has been imported.

    ArrayList<Integer> array = new ArrayList<Integer>();
    array.add(1);
    array.add(9);
    array.add(10);
    array.remove(1);
    array.add(1);
    array.set(0, 3);
    int num = array.get(1);
    System.out.println(num);
    Plain text

Exercise: ArrayList of Odd Numbers

Create an ArrayList with elements that are odd numbers from 101 - 199.

Hint: Use a loop!