πŸ““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

  1. Go to the public template repository for our class: BWL-CS Java Template
  2. Click the button above the list of files then select Create a new repository
  3. Specify the repository name: CS2-Unit-7-Notes
  4. Click

    Now you have your own personal copy of this starter code that you can always access under the Your repositories section of GitHub!

  5. Now on your repository, click and select the Codespaces tab
  6. Click Create Codespace on main and wait for the environment to load, then you’re ready to code!
  7. πŸ“ 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.

image

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 Strings 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 declared ArrayList<Object>, but it is good practice to specify the type of objects you intend to store in an ArrayList 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 a ArrayList object in memory. It only creates a variable that can refer to a ArrayList or null. To actually create a ArrayList we must invoke a constructor such as new 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.

ArrayLists 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

  • ArrayLists are re-sizable lists that allow adding and removing items to change their size during run time.

  • The ArrayList class is in the java.util package. You must import java.util.ArrayList or java.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 constructor ArrayList() constructs an empty list of size 0.

  • Java allows the generic type ArrayList<E>, where the generic type E specifies the type of the elements, like String or Integer. Without it, the type will be Object.

  • ArrayList<E> is preferred over ArrayList 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 type E.

  • ArrayLists cannot hold primitive types like int or double, so you must use the wrapper classes Integer or Double to put numerical values into an ArrayList. However autoboxing usually takes care of that for you.

πŸ›‘ When class ends, don’t forget to SAVE YOUR WORK!

  1. Navigate to the Source Control menu on the LEFT sidebar
  2. Type a brief commit message in the box, for example: updated Main.java
  3. Click the button on the LEFT menu
  4. Click the button on the LEFT menu
  5. Finally you can close your Codespace!

Acknowledgement

Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.