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 List on the CS A exam

Import package: 7.1.1

  • You do not need to worry about import statements on the AP exam!
  • ArrayList class is housed in the java.util package
    • A package is a set/library of related classes
    • java.lang is 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
  • 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
  • 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.*;

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
    • The java compiler will warn you if you don’t specify a type
    • it breaks type safety

Example of Compiler warning

  • To Create an ArrayList, use new ArrayList<Type>()

Using Primitive values

  • Arraylists can only hold Objects, so primitive types must be wrapped in objects
    • int becomes Integer
    • double becomes Double
    • 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 NullPointerException when 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