represent collections of related object reference data using ArrayList objects
In the last unit, we learned about arrays to hold collections of related data.
But arrays have limitations.
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 add and remove items from the collection and change the size of the collection while the programming is running?
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.
At the beginning of the week, you probably wouldn't know how many items will end up being on the list.
Luckily, Java has a class called ArrayList which is a re-sizeable array.
An ArrayList has an underlying array that grows or shrinks as needed.
You can use ArrayList instead of arrays when:
You don't know the size of the array you need
You know that you will add and remove items
You may need to change the array's size dynamically during run time.
An ArrayList is mutable, meaning it can change during runtime by adding and removing objects from it.
An ArrayList is often called a List on the exam.
If you see List being used on the exam, just assume it's an ArrayList.
IMPORT PACKAGE
The ArrayList class is in the java.util package.
A package is a set or library of related classes.
The java.lang package is the main Java language classes that you get automatically without importing it.
The java.util package has a lot of utility classes that you can use if you import the package.
If you want to use and class other than those in java.lang you will need to either use the full name like java.util.ArrayList, or use one or more import statements to import in that package.
Import statements have to be the first code in a Java source file.
An import statement tells Java which class you mean when you use a short name (like ArrayList).
It tells Java where to find the definition of that class.
You can import just the classes you need from a package like this:
import java.util.ArrayList; // import just the ArrayList package
Another option is to import everything at the same level in a package like this:
import java.util.*; // import everything in package including ArrayList
Don't worry about adding import statements on the exam, any that you need will be provided for you.
DECLARING AND CREATING ARRAYLISTS
To declare an ArrayList use ArrayList<Type> name.
Change the Type to be whatever type of objects you want to store in the ArrayList.
NOTE: ArrayLists can only hold objects like String and the wrapper classes Integer and Double. They cannot hold primitive types like int, double, etc.
It is not required to specify the generic type <Type>, since it will default to Object, but it is a good practice to specify it to restrict what to allow in your ArrayList.
Using a type ArrayList<Type> is preferred over just using ArrayList because it allows the compiler to find errors that would otherwise be missed until run-time.
Notice above that I declared a variable called nameList that can refer to a ArrayList of strings, but it currently doesn't refer to any ArrayList yet (it's set to null).
Keep in mind that declaring a ArrayList doesn't actually create a ArrayList.
It only creates a variable that can refer to a ArrayList.
To actually create a ArrayList use new ArrayList<Type>().
If you leave off the <Type> it will default to Object.
The shoppingList ArrayList was declared using the new keyword but is empty. When I printed out shoppingList it just prints out a set of square brackets [].
You can get the number of items in a ArrayList using the size() method:
Notice that an empty ArrayList has a size of 0 because the ArrayList constructor constructs an empty list.
Also, you can't get the size of a ArrayList that is currently set to null.
You will get a NullPointerException instead, meaning that you tried to do something with an object reference that was null (doesn't exist).
I created a ArrayList of Integer values above.
I had to use Integer as the type because ArrayLists can only hold objects, not primitive values.
All primitive types (int, double, etc.) 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.
You can actually put any kind of Objects in an ArrayList, even for a class that you wrote yourself.
CONVERTING ARRAYS TO ARRAYLISTS
You can convert arrays to ArrayLists using its constructor with an argument Arrays.asList(arrayname) like the following:
Note that ArrayLists have a toString() method that is automatically called to print the list in a nice format.
ADD METHOD
You can add values to an ArrayList by using its add method.
The add method will be described in detail in the next lesson.
Note that the type of ArrayList, String or Integer, also determines the type of parameters and return types for all of its methods, so add and print work for any type of ArrayList.
SUMMARY
ArrayList are re-sizable arrays 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.* 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 prarameters 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.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) This programming challenge is based on the 2017 FRQ part 1a on the 2017 exam. In this question, you are asked to write a constructor for a class called Digits. This constructor takes an integer number as its argument and divides it up into its digits and puts the digits into an ArrayList. For example, new Digits(154) creates an ArrayList with the digits [1, 5, 4].
You can use a special method called Collections.reverse(digitsList); to reverse the order of the digits in the ArrayList after the loop to get them in the right order.
Your task is to create a program with a Digits class (and Digits constructor) that functions as described above. Here is some code to help you get started:
public class Main
{
public static void main(String[] args)
{
Digits d1 = new Digits(154);
System.out.println(d1);
}
}
import java.util.*;
public class Digits
{
// instance variable is a list of digits
private ArrayList<Integer> digitList;
// constructor (unfinished)
public Digits (int number)
{
// make sure to initialize digitList to an empty ArrayList of Integers
// Use a while loop to add each digit in number to digitList
// Use Collections.reverse(digitList); to reverse the digits
}
// method to return the string representation of the digits list