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.
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.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Add a few more classmates to
studentList. Predict the output of the code and run it to determine if your prediction is correct.
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.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Add "Susie" at index 1. Predict the output of the code and run it to determine if your prediction is correct.
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.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Update the found to look for "David". Predict the output of the code and run it to determine if your prediction is correct.
E get (int index)
The method returns the element at index.
Your Turn
Let's try it in the Java Playground.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Write code to get the first and last elements of
studentList. Predict the output of the code and run it to determine if your prediction is correct.
- Write code to attempt to get the value at element
5. What happens when you attempt to access an element that does not exist?
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.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Write code to obtain the index for "David". Predict the output of the code and run it to determine if your prediction is correct.
- Write code to obtain the index for "Susie". Predict the output of the code and run it to determine if your prediction is correct.
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.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
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.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Write code to remove the first element of
studentList. Predict the output of the code and run it to determine if your prediction is correct.
- Write code to remove the element at index 3 of
studentList. Predict the output of the code and run it to determine if your prediction is correct.
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.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Attempt to remove "Susie" from the list. Predict the output of the code and run it to determine if your prediciton is correct.
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.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Write code to change the first element of
studentList to "John". Predict the output of the code and run it to determine if your prediction is correct.
int size ()
The method returns the number of elements in the ArrayList.
Your Turn
Let's try it in the Java Playground.
- Predict the output of the code.
- Run the code to determine if your prediction is correct.
- Add more student names to
studentList. Predict the output of the code and run it to determine if your prediciton is correct.
Resources