Year 11 Computer Science
Unit 8: ArrayLists
learning objectives
➢
➢
➢
➢
➢
Understand why an ArrayList is a dynamic data structure.
Understand that ArrayLists contain non-primitive data.
Declare an ArrayList.
Instantiate an ArrayList.
Understand and use methods in the ArrayList class such as
add(Object element), isEmpty(), size(), get(int index),
set(int index, Object element), and clear().
➢ Know how to sort an ArrayList using the Collections class.
ArrayList - Declaration
➢ The ArrayList class will need to be imported into our program
using the statement
import java.util.ArrayList;
these are called diamond brackets
(angle brackets or generics)
➢ Declaring an ArrayList:
ArrayList < String > myArrayList;
the name of Arraylist
type declaration
➢ We should specify the type of data that the ArrayList will contain.
Just like when working with arrays, ArrayLists should contain a
specific type of data.
ArrayList
We have worked with arrays in the past
drawbacks of using arrays :😕
➢ We need to know the size of the array in advance.
➢ The length of the array is fixed (it is a static data structure).
➢ If the array becomes full, we need to create a new array of a
larger capacity and copy the elements over.
ArrayLists help us deal with these limitations.😉
ArrayList - Instantiation
ArrayList<String> myArrayList;
Equivalent
myArrayList = new ArrayList<String>();
Or, in a single statement that includes declaration and instantiation
ArrayList<String> myArrayList = new ArrayList<String>();
ArrayList - isEmpty()
What will be printed? true
ArrayList<String> studentsWhoEscaped = new ArrayList<String>();
System.out.println(studentsWhoEscaped.isEmpty());
studentsWhoEscaped.add(“Miya”);
studentsWhoEscaped.add(“Natalie”);
studentsWhoEscaped.add(“Hanyi”);
studentsWhoEscaped.add(“Warren”);
What will be printed? false
studentsWhoEscaped.add(“Oceana”);
System.out.println(studentsWhoEscaped.isEmpty());
ArrayList - add(Object element)
ArrayList<String> studentsWhoEscaped = new ArrayList<String>();
studentsWhoEscaped.add(“Miya”);
studentsWhoEscaped.add(“Natalie”);
studentsWhoEscaped.add(“Hanyi”);
studentsWhoEscaped.add(“Warren”);
What will get printed?
studentsWhoEscaped.add(“Oceana”);
System.out.println(studentsWhoEscaped);
The type of data that we add to the array list must confirm to the
declaration type inside the <>.
output: [“Miya”, “Natalie”, “Hanyi”, “Warren”, “Oceana”]
ArrayList - add(int index, Object element)
ArrayList<String> studentsWhoEscaped = new ArrayList()<String>;
studentsWhoEscaped.add(“Miya”);
studentsWhoEscaped.add(“Natalie”);
studentsWhoEscaped.add(0,“Hanyi”);
studentsWhoEscaped.add(“Warren”);
What will get printed?
studentsWhoEscaped.add(“Oceana”);
System.out.println(studentsWhoEscaped);
Note that this does not delete the element that already exists at index 0, it
simply pushes all elements at or above index to the right.
output: [“Hanyi”, “Miya”, “Natalie”, “Warren”, “Oceana”]
ArrayList - size()
ArrayList<String> studentsWhoEscaped = new ArrayList<String>();
System.out.println(studentsWhoEscaped.size());
studentsWhoEscaped.add(“Miya”);
studentsWhoEscaped.add(“Natalie”);
What will get printed? 0
studentsWhoEscaped.add(0,“Hanyi”);
studentsWhoEscaped.add(“Warren”);
studentsWhoEscaped.add(“Oceana”);
What will get printed? 5
System.out.println(studentsWhoEscaped.size());
ArrayList - get(int index)
ArrayList<String> studentsWhoEscaped = new ArrayList<String>();
studentsWhoEscaped.add(“Miya”);
studentsWhoEscaped.add(“Natalie”);
studentsWhoEscaped.add(0,“Hanyi”);
? Hanyi
studentsWhoEscaped.add(“Warren”);
studentsWhoEscaped.add(“Oceana”);
? Miya
System.out.println(studentsWhoEscaped.get(0));
System.out.println(studentsWhoEscaped.get(1));
System.out.println(studentsWhoEscaped.get(studentsWhoEscaped.size()-1));
? Oceana
ArrayList - remove(int index)
ArrayList<String> studentsWhoEscaped = new ArrayList<String>();
studentsWhoEscaped.add(“Miya”);
studentsWhoEscaped.add(“Natalie”);
studentsWhoEscaped.add(0,“Hanyi”);
studentsWhoEscaped.add(“Warren”);
What will be printed?
studentsWhoEscaped.add(“Oceana”);
System.out.println(studentsWhoEscaped);
[“Hanyi”, “Miya”, “Natalie”, “Warren”, “Oceana”]
studentsWhoEscaped.remove(0);
What will be printed?
System.out.println(studentsWhoEscaped);
[“Miya”, “Natalie”, “Warren”, “Oceana”]
ArrayList - set(int index, Object element)
ArrayList<String> studentsWhoEscaped = new ArrayList<String>();
studentsWhoEscaped.add(“Miya”);
studentsWhoEscaped.add(“Natalie”);
studentsWhoEscaped.add(0,“Hanyi”);
studentsWhoEscaped.add(“Warren”);
What will be printed?
studentsWhoEscaped.add(“Oceana”);
System.out.println(studentsWhoEscaped);
[“Hanyi”, “Miya”, “Natalie”, “Warren”, “Oceana”]
studentsWhoEscaped.set(2, “Haoran”);
What will be printed?
System.out.println(studentsWhoEscaped);
[“Hanyi”, “Miya”, “Haoran”, “Warren”, “Oceana”]
ArrayList - clear()
ArrayList<String> studentsWhoEscaped = new ArrayList<String>();
studentsWhoEscaped.add(“Miya”);
studentsWhoEscaped.add(“Natalie”);
studentsWhoEscaped.add(0,“Hanyi”);
studentsWhoEscaped.add(“Warren”);
studentsWhoEscaped.add(“Oceana”);
5
?
System.out.println(studentsWhoEscaped.size());
studentsWhoEscaped.clear();
System.out.println(studentsWhoEscaped.size());
?
0
ArrayList - Sorting Using the Collections Class
ArrayList<String> studentsWhoEscaped = new ArrayList<String>();
studentsWhoEscaped.add(“Natalie”);
studentsWhoEscaped.add(0,“Hanyi”);
studentsWhoEscaped.add(“Warren”);
[“Hanyi”, “Natalie”, “Warren”, “Oceana”]
studentsWhoEscaped.add(“Oceana”); ?
System.out.println(studentsWhoEscaped);
Collections.sort(studentsWhoEscaped);
System.out.println(studentsWhoEscaped); ?
[“Hanyi”, “Natalie”, “Oceana”, “Warren”]
ArrayList - Non-Primitive Types Only
ArrayLists contain non-primitive types (reference types) only.
ArrayList<int> grades = new ArrayList<int>(); ❌ will not compile.int
is primitive
will compile. Integer is non-primitive
ArrayList<Integer> grades = new ArrayList<Integer>();✅
Note the difference in the above two declaration and instantiation
statements.
We can only insert a non-primitive type within the diamond
brackets <>.
ArrayList - Wrapper Classes
ArrayList<Integer> grades = new ArrayList<Integer>();
➢
➢
➢
➢
➢
ArrayLists contain non-primitive types (reference
types) only.
Integer is something that is known as a wrapper
class.
Each of the eight primitive types has a
corresponding wrapper class.
The purpose of a wrapper class is to provide a
non-primitive representation of a primitive
type.
These wrapper classes allow us to (effectively)
add primitive type data to ArrayLists.
W3Schools
ArrayList - Boxing and Unboxing - Boxing
ArrayList<Integer> grades = new ArrayList<Integer>();
grades.add(90);
grades.add(78);
➢ ArrayLists contain non-primitive types (reference types) only.
➢ Note that the types of the literal values that have been passed to
the add methods are type int (a primitive type).
➢ Java will automatically box / wrap (convert) the primitive int type
to a non-primitive Integer type before adding it to the array list.
ArrayList - Boxing and Unboxing - Unboxing
ArrayList<Integer> grades = new ArrayList<Integer>();
grades.add(90);
grades.add(78);
int firstGrade = grades.get(0);
➢ ArrayLists contain non-primitive types (reference types) only.
➢ Java will automatically unbox / unwrap (convert) the
non-primitive Integer type stored at index 0 to a primitive int
type before returning the value. Hence, we can store the returned
value in a variable of type int.
ArrayList - Iterating - While Loop
ArrayList<Integer> grades = new ArrayList<Integer>();
grades.add(90);
grades.add(78);
int index = 0;
while(index < grades.size()) {
System.out.println(grades.get(index++));
}
➢ We can iterate through an arraylist using a standard loop or an
enhanced for loop.
ArrayList - Iterating - For Loop
ArrayList<Integer> grades = new ArrayList<Integer>();
grades.add(90);
grades.add(78);
for(int i = 0; i < grades.size(); i++) {
System.out.println(grades.get(i));
}
➢ We can iterate through an arraylist using a standard loop or
an enhanced for loop.
ArrayList - Iterating - Enhanced For Loop
ArrayList<Integer> grades = new ArrayList<Integer>();
grades.add(90);
grades.add(78);
for(Integer grade: grades) {
System.out.println(grade);
}
➢ We can iterate through an arraylist using a standard loop or
an enhanced for loop.