Sub topic 14
Choosing Between Arrays and Collections
An array and a collection can both hold several values. The main choice is whether the number of values needs to change.
An array has a fixed length. You can change its elements, but you cannot add or remove positions after creating it. A collection is an object that groups items. Some collections can grow and shrink as the program runs.
Start with the task
| Task | A useful choice | Why |
|---|---|---|
| Store exactly seven daily temperatures | `int[]` | The number of positions is known and stays fixed. |
| Add scores as players join | `ArrayList<Integer>` | Items can be added or removed. |
| Pass values to a method that requires `int[]` | `int[]` | The method expects an array. |
| Store names without duplicates | A `Set<String>` | A set keeps only one copy of each value. |
A list is a kind of collection that keeps items in order and allows repeated items. An ArrayList is a list that can grow and shrink. A set is a collection that does not allow duplicate items.
Arrays can hold primitive values, such as int, directly. A primitive is a basic value type in Java. Collections hold objects instead. For a list of whole numbers, use Integer, the object type for an int.
The type inside angle brackets says what kind of item a collection holds. Write List<Integer>, not List<int>. Java normally changes between int and Integer for you when needed.
Work through an example
Suppose a game has three fixed target scores. Players can also add scores during the game. The targets fit in an array, while an ArrayList fits the changing scores.
Save this program as ScoreChoices.java:
import java.util.ArrayList;
import java.util.List;
public class ScoreChoices {
public static void main(String[] args) {
int[] targets = {80, 90, 100};
List<Integer> scores = new ArrayList<>();
scores.add(80);
scores.add(90);
scores.add(95);
System.out.println("First target: " + targets[0]);
System.out.println("Number of targets: " + targets.length);
System.out.println("Scores: " + scores);
scores.remove(0);
System.out.println("Scores after removal: " + scores);
System.out.println("Number of scores: " + scores.size());
}
}Compile and run it:
javac ScoreChoices.java
java ScoreChoicesThe first target is 80, and the array has three positions. The list initially prints [80, 90, 95]. After remove(0), it prints [90, 95] and has two items.
List<Integer> describes the kind of list the variable holds. new ArrayList<>() creates the actual list. Use length for an array and size() for a list.
Watch for trade-offs
Choose an array when its fixed length is useful, or when another method requires one. Choose an ArrayList when you need to add or remove items. Both let you get an item by its position.
An array of primitives stores those values directly. An ArrayList<Integer> uses Integer objects, which can need more memory. However, do not assume an array will always make a program noticeably faster. Choose the structure that fits the task first.
An Integer item can also be null, meaning it refers to no object. A value in an int[] cannot be null.
Practice
Exercise
A program records a shopping list. Users can add items and remove them after buying them. Would you choose an array or an ArrayList, and why?
Show answer
Choose an ArrayList because the number of items changes. It supports adding and removing items without creating a new array.
Exercise
A method requires an int[] containing exactly four readings. Would an ArrayList<Integer> be a direct replacement?
Show answer
No. The method requires an int array, so pass an int[] with four elements.