Intro to ArrayLists
Intro to ArrayLists: 7.1.0
- Arrays have Limitations
- The size of an array can’t be changed after declaration
ArrayLists are resizable- The underlying array grows and shrinks as needed
- This makes an arraylist
mutable - Often called a
Liston the CS A exam
Import package: 7.1.1
- You do not need to worry about import statements on the AP exam!
ArrayListclass is housed in thejava.utilpackage- A package is a set/library of related classes
java.langis the package of main java classes; it is automatically imported- You need to manually import everything else
- You can also use the full name of the class
java.util.ArrayList
- You can also use the full name of the class
- You need to manually import everything else
- Import statements are the first bits of code in a Java source file
- This tells java what you mean when you use a short name like
ArrayList, and where to find the underlying class- This adds the class to your namespace!
- This is useful because there can be many classes with the same short name, and it specifies which one you actually mean
- This tells java what you mean when you use a short name like
- There are a few ways to import
- Importing just the class you want
import java.util.ArrayList;
- Importing all classes in a given package
import java.util.*;
- Importing just the class you want
Declaring and Creating ArrayLists: 7.1.2
ArrayList<Type> name- Type is whatever object you want to store
- You don’t have to specify a type because it’ll default to Object
- All classes extend
java.lang.Object - It is good practice to specify one anyway
- You can control what is actually allowed in the ArrayList
- All classes extend
- The java compiler will warn you if you don’t specify a type
- it breaks type safety

- To Create an ArrayList, use
new ArrayList<Type>()
Using Primitive values
- Arraylists can only hold Objects, so primitive types must be
wrappedin objectsintbecomesIntegerdoublebecomesDouble- etc.
- All kinds of objects can be used in an ArrayList
- Even ones you write yourself!
Converting arrays to lists
This isn’t covered on the exam!
String divisions = {"Carver", "Galileo", "Hopper", "Newton", "Roebling", "Turing"};
ArrayList<String> frcDivisions = new ArrayList<String>(Arrays.asList(divisions));
System.out.println(frcDivisions);
Other Arraylist functions
- The number of items in a list can be found using
.size()- An empty arraylist has a size of 0
- you get a
NullPointerExceptionwhen calling.size()on a null arraylist- You can’t do something on an object which doesn’t exist!
- You can add values to a list using
.add()
Summary: 7.1.4
- ArrayLists are resizable arrays
- The ArrayList class is found in
java.util- It must be imported to be used
new ArrayList()constructs an ArrayList with a size of 0- a type can be specified when constructing an arraylist
- This is preferred so the compiler can find errors ahead of time
- Arraylists can only hold objects - not primitives
- Primitives must first be
wrapped
- Primitives must first be

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
This was adapted from the CS Awesome curriculum, which was created by
Barbara Ericson, Beryl Hoffman, and many other CS Awesome contributors. All rights reserved.
CS Awesome is licensed under CC BY-NC-SA 4.0.