Skip Top Navigation Bar

Introduction to List and ArrayList Objects

Have you ever made a list? kept a budget? used a spreadsheet? These are collections of related items we call a list. One way that we can represent this in a program is by using a List, specifically an ArrayList.

An ArrayList is a resizable list, meaning it shrinks and grows as needed.

How Data is Stored?

Each value stored in an ArrayList is called an element. As elements are added to the ArrayList the size of the ArrayList increases by 1.

Similar to the characters in a String, each element in an ArrayList has an index. The first index is 0 and the last is the size of the ArrayList - 1.

Constructing an ArrayList

When constructing an ArrayList object, we must specify the type of data that is stored. An ArrayList only stores reference data. Wrapper classes will be used to store any primitive data.

General Format for Constructing an ArrayList

ArrayList< <type of reference data> > name = new ArrayList<> ();

An example of an ArrayList of student names in a class would be as follows:

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

An example of an ArrayList of a student's grades would be as follows:

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

boolean add (E e)

Appends the specified element to the end of the list. E is the type of the elements in the ArrayList.

The method returns true if the element has been added to the list.

Often the return value is not captured when using ArrayList.

Your Turn

Let's try it in the Java Playground.

void add (int index, E e)

Inserts the specified element at index. All current elements are shifted to the right. E is the type of the elements in the ArrayList.

The method does not return a value.

Your Turn

Let's try it in the Java Playground.

boolean contains(Object o)

This method returns true if Object o is found in the ArrayList; false otherwise.

Your Turn

Let's try it in the Java Playground.

E get (int index)

The method returns the element at index.

Your Turn

Let's try it in the Java Playground.

int indexOf(Object o)

This method return the index of the first occurrence of Object o if it exists. If the ArrayList does not contain this value, the method returns -1.

Your Turn

Let's try it in the Java Playground.

boolean isEmpty()

The method returns true if there are no elements in the ArrayList and false otherwise.

Your Turn

Let's try it in the Java Playground.

E remove (int index)

The method removes the element at the given index and returns the element. Any elements to the right of this element are shifted to the left and the size of the ArrayList is decreased by 1.

Your Turn

Let's try it in the Java Playground.

booleam remove (Object o)

The method removes the first occurrence of Object o if it exists and returns true. If Object o does not exist, the ArrayList is unchanged and false is returned.

Your Turn

Let's try it in the Java Playground.

E set (int index, E e)

The method changes the value at index to be e and returns the previous value at index.

Your Turn

Let's try it in the Java Playground.

int size ()

The method returns the number of elements in the ArrayList.

Your Turn

Let's try it in the Java Playground.

Resources