π4.8: ArrayList Class
Table of Contents
π This page is a condensed version of CSAwesome Topic 4.8
β΄β΄β΄ NEW UNIT/SECTION! β΄β΄β΄
Create a blank Java program to take your class notes in for the next few lessons.
Click on the collapsed heading below for GitHub instructions ‡
π NOTES PROGRAM SETUP INSTRUCTIONS
- 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-Unit4PartB-Notes
- For the description, write:
ArrayList data collections
- 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, writing code & comments along with the instructor.
π When class ends, donβt forget to SAVE YOUR WORK! Codespaces are TEMPORARY editing environments, so you need to COMMIT changes properly in order to update the main repository for your program.
There are multiple steps to saving in GitHub Codespaces:
- Navigate to the
Source Control
menu on the LEFT sidebar - Click the button on the LEFT menu
- Type a brief commit message at the top of the file that opens, for example:
updated Main.java
- Click the small
βοΈ
checkmark in the TOP RIGHT corner - Click the button on the LEFT menu
- Finally you can close your Codespace!
ArrayLists
An ArrayList is a resizable list in Java that can store objects. Unlike arrays, ArrayLists can change their size during runtime by adding or removing elements. They are part of the java.util
package.
Note: ArrayLists can only store object references, not primitive types like int
or double
. Use wrapper classes such as Integer
or Double
instead. Javaβs autoboxing feature often handles this conversion automatically.
Importing and Creating ArrayLists
To use ArrayLists, import them:
import java.util.ArrayList;
You can also import all classes in java.util
:
import java.util.*;
Create an ArrayList using:
ArrayList<Type> name = new ArrayList<Type>();
For example:
ArrayList<String> names = new ArrayList<String>();
Adding Items to an ArrayList
Use the .add()
method to append elements:
Type this into your Codespace, run it, and observe the output.
import java.util.*;
public class AddExample {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
names.add("Diego");
names.add("Grace");
names.add("Deja");
System.out.println(names);
}
}
Adding and Removing by Index
You can specify the index where an item should be added:
list.add(index, value);
To remove an item by index:
list.remove(index);
Example:
Modify and run this code. Try adding items at different indices and removing them.
import java.util.*;
public class AddRemoveExample {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(1, 5); // inserts 5 at index 1
System.out.println(numbers);
numbers.remove(2); // removes element at index 2
System.out.println(numbers);
}
}
Getting and Setting Elements
Use .get(index)
to retrieve an element and .set(index, value)
to replace it.
Type this in and make changes to see the results.
import java.util.*;
public class GetSetExample {
public static void main(String[] args) {
ArrayList<String> nameList = new ArrayList<String>();
nameList.add("Diego");
nameList.add("Grace");
nameList.add("Deja");
System.out.println(nameList);
System.out.println(nameList.get(0));
System.out.println(nameList.get(1));
nameList.set(1, "John");
System.out.println(nameList);
}
}
Comparing Arrays and ArrayLists
When to use arrays:
- You know the number of elements ahead of time
- The order and number of items will not change
When to use ArrayLists:
- The number of elements can change
- You need to frequently add or remove items
Example:
// Arrays - must specify a size
int[] highScores = new int[5];
String[] names = new String[5];
// ArrayLists - start empty
ArrayList<Integer> highScoreList = new ArrayList<Integer>();
ArrayList<String> nameList = new ArrayList<String>();
Operation | Array Syntax | ArrayList Syntax |
---|---|---|
length/size | array.length | list.size() |
Access | value = array[index]; | value = list.get(index); |
Modify | array[index] = value; | list.set(index, value); |
Practice: Convert Array to ArrayList
Rewrite this code to use an ArrayList instead of an array. Comment why an ArrayList is better for this problem.
import java.util.*;
public class ToDoList {
public static void main(String[] args) {
String[] toDoList = new String[3];
toDoList[0] = "Do homework";
toDoList[1] = "Help make dinner";
toDoList[2] = "Call grandma";
toDoList[1] = "Order pizza";
System.out.println(toDoList.length + " things to do!");
System.out.println("Here's the first thing to do: " + toDoList[0]);
toDoList[0] = toDoList[1];
toDoList[1] = toDoList[2];
toDoList[2] = "";
System.out.println("Here's the next thing to do: " + toDoList[0]);
}
}
Creating an ArrayList from an Array
import java.util.*;
public class ArrayListFromArray {
public static void main(String[] args) {
String[] names = {"Dakota", "Madison", "Brooklyn"};
ArrayList<String> namesList = new ArrayList<String>(Arrays.asList(names));
System.out.println(namesList);
}
}
Coding Challenge: FRQ Digits
Construct a Digits
class that breaks an integer into its digits and stores them in an ArrayList in the correct order.
Write the constructor using a loop. Use add(0, value)
to insert at the beginning so digits are in correct order.
import java.util.*;
public class Digits {
private ArrayList<Integer> digitList;
public Digits(int number) {
// initialize digitList
// use while loop and % / operators to get digits
// use add(0, digit) to insert at front
}
public String toString() {
return digitList.toString();
}
public static void main(String[] args) {
Digits d1 = new Digits(154);
System.out.println(d1);
}
}
Summary
- ArrayLists are resizable lists for storing objects.
- Part of
java.util
package. - Use generics:
ArrayList<E>
to specify type. - Cannot store primitives directlyβuse wrapper classes.
-
Key methods:
size()
add(E obj)
add(int index, E obj)
remove(int index)
get(int index)
set(int index, E obj)
- Use arrays when size is fixed; ArrayLists when size changes.
Acknowledgement
Content on this page is adapted from Runestone Academy - Barb Ericson, Beryl Hoffman, Peter Seibel.