
AP CS Name: ________________________
Notes: ArrayLists
ArrayList: (BJP Chapter 10)
list: a collection storing an ordered sequence of elements
each element is accessible by a 0-based index
a list has a size (number of elements that have been added)
elements can be added to the front, back, or elsewhere
in Java, a list can be represented as an ArrayList object
Required Library for these: (placed above the Class creation)
ArrayList:
Required library for Arrays (placed about the class creation):
import java.util.*;
ArrayList creation and use:
ArrayList<Type> name = new ArrayList<Type>();
name.add("Your Name"); // Assigning a value to the end of an ArrayList name
public static type methodName(ArrayList<Type> name){ // ArrayList as a parameter
public static ArrayList<Type> methodName(parameters) { // returning an ArrayList
methodName(arrayListName); //calling a method with an ArrayList as its parameter
ArrayList<String> names = methodName(parameters); // ArrayList as returned value
Examples:
ArrayList<String> names = new ArrayList<String>(); // an ArrayList of Strings
public static void actOnIt(ArrayList<String> input) {} // ArrayList parameter
// returning a boolean ArrayList, using “Integer” as the wrapper for int
public static ArrayList<Integer> figureItOut(int x, int y) {}
actOnIt(names); // calling a method with an array as its parameter
ArrayList<Integer> values = figureItOut(x, y); // ArrayList as a returned value