π7.1: ArrayList Basics
Table of Contents
π This page is a condensed version of CSAwesome Topic 7.1
Using a GitHub Template for class notes
- Go to the public template repository for our class: BWL-CS Java Template
- Click the button above the list of files then select
Create a new repository
- Specify the repository name:
CS2-Unit-7-Notes
- Click
Now you have your own personal copy of this starter code that you can always access under the
Your repositories
section of GitHub! - Now on your repository, click and select the
Codespaces
tab - Click
Create Codespace on main
and wait for the environment to load, then youβre ready to code! - π Take notes in this Codespace during class, coding along with the instructor.
Intro to ArrayLists
In the last unit, we learned about using arrays to hold collections of related data. However arrays are not very flexible. Most notably, the size of an array is established at the time of creation and cannot be changed. What if you donβt know how big the collection of data will be? What if you want to both add and remove items from a collection?
For example, if you wanted to represent a shopping list, you might add to the list throughout the week and remove things from the list while you are shopping. You probably would not know how many items will be on the list at the beginning of the week.
You can use ArrayList
instead of arrays whenever you donβt know the size of the array you need or you know that you will add and remove items and may need to change the arrayβs size dynamically during run time. An ArrayList
is mutable, meaning it can change during run time by adding and removing objects from it.
- ArrayList
- A re-sizable collection. It is called
ArrayList
because it stores the items that have been added to it in an underlying array. But it also takes care of keeping track of how many items have been added to the array and it will create a new bigger array under the covers when needed to hold more items.
- Mutable
- Can change during run time by adding and removing objects from it.
Packages and Imports
The ArrayList
class is in the java.util
package. A package is a set or library of related classes. The classes we have used until now, such as String
and Math
, are in the special package java.lang
whose classes are always available in any Java program. Other packages, such as java.util
, provide classes that can only be used either by importing them or (much more rarely) by referring to them by their full name which includes the package as a prefix.
Import statements have to come before the class
definition in a Java source file and serve to tell Java which class you mean when you use a short name like ArrayList
. To import just one class we use a single import
of the fully-qualified name of the class like this:
import java.util.ArrayList;
AP EXAM NOTE: Donβt worry about adding
import
statements on the exam. Any that you need will be provided for you.
After such an import statement, anywhere ArrayList
is used as a class name in the file it will be taken to mean java.util.ArrayList
.
Declaring and Creating ArrayLists
To declare a ArrayList use ArrayList<Type> name
where Type, called a type parameter is the type of objects you want to store in the ArrayList.
For example a variable naming an ArrayList
meant to hold String
s is declared as ArrayList<String>
as shown in the code below.
You can declare a variable to just be of type
ArrayList
, with no type parameter, and itβll be approximately the same as if you had declaredArrayList<Object>
, but it is good practice to specify the type of objects you intend to store in anArrayList
as it allows the compiler to find errors that would otherwise be missed until run time.
// ArrayList<Type> name = new ArrayList<Type>();
// An ArrayList of Strings:
ArrayList<String> shoppingList = new ArrayList<String>();
As with other reference types, declaring a
ArrayList
variable doesnβt actually create aArrayList
object in memory. It only creates a variable that can refer to aArrayList
ornull
. To actually create aArrayList
we must invoke a constructor such asnew ArrayList<String>()
.
You can get the number of items in a ArrayList
using the size()
method. Notice that a newly constructed ArrayList
is empty and thus has a size of 0.
ArrayList
s can only hold reference types (objects) like String
. Since they canβt hold primitive types like int
and double
, if we want a collection of numbers we need to use the wrapper classes Integer
or Double
.
Wrapper Classes: Integer & Double
You can also create ArrayLists of integer and double values. However, you have to use Integer
or Double
as the type parameter because ArrayList
\ s can only hold objects, not primitive values. All primitive types must be wrapped in objects before they are added to an ArrayList. For example, int
values can be wrapped in Integer
objects, double
values can be wrapped in Double
objects. However this normally happens automatically thanks to autoboxing.
ArrayList<Integer> numList = new ArrayList<Integer>();
ArrayList<Double> decimalList = new ArrayList<Double>();
You can actually put in any kind of objects in an ArrayList
, including instances of classes that you write, such as the Student
, Person
, or Creature
classes from Unit 5.
βοΈ Summary
-
ArrayList
s are re-sizable lists that allow adding and removing items to change their size during run time. -
The
ArrayList
class is in thejava.util
package. You must importjava.util.ArrayList
orjava.util.*
to use it. -
An
ArrayList
object contains object references and is mutable, meaning it can change (by adding and removing items from it). -
The
ArrayList
constructorArrayList()
constructs an empty list of size 0. -
Java allows the generic type
ArrayList<E>
, where the generic typeE
specifies the type of the elements, likeString
orInteger
. Without it, the type will beObject
. -
ArrayList<E>
is preferred overArrayList
because it allows the compiler to find errors that would otherwise be found at run time. -
When
ArrayList<E>
is specified, the types of the reference parameters and return type when using its methods are typeE
. -
ArrayList
s cannot hold primitive types likeint
ordouble
, so you must use the wrapper classesInteger
orDouble
to put numerical values into anArrayList
. However autoboxing usually takes care of that for you.
π When class ends, donβt forget to SAVE YOUR WORK!
- Navigate to the
Source Control
menu on the LEFT sidebar - Type a brief commit message in the box, for example:
updated Main.java
- Click the button on the LEFT menu
- Click the button on the LEFT menu
- Finally you can close your Codespace!
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.