Sub topic 3
Ordered Sequences with List and ArrayList
What a List Is
A List is a collection that keeps its elements in order. An element is one item stored in the collection. Each element has an index, which is its position number. The first index is 0, just like in an array.
A List has two key features:
- It keeps elements in the order you added them.
- It allows duplicates, so the same value can appear more than once.
List is an interface. That means it describes what a list can do but does not hold any data itself. To get a real list, you need a class that implements the interface. The most common one is ArrayList, a list that stores its elements in an array behind the scenes and grows that array for you when it fills up.
Why ArrayList Beats a Plain Array
An array has a fixed size. Once you create new String[3], it can never hold a fourth item. An ArrayList starts empty and grows as you add elements. It also gives you handy methods for adding, finding, and removing items.
A Worked Example
This program builds a list of names, reads one, changes one, and checks the size.
import java.util.ArrayList;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Ana");
names.add("Ben");
names.add("Ana");
System.out.println(names); // [Ana, Ben, Ana]
System.out.println(names.get(1)); // Ben
names.set(1, "Cara");
System.out.println(names); // [Ana, Cara, Ana]
System.out.println(names.size()); // 3
}
}Here is what each step does:
add("Ana")puts "Ana" at the end of the list.- Adding "Ana" a second time works, because lists allow duplicates.
get(1)returns the element at index 1, which is "Ben".set(1, "Cara")replaces the element at index 1.size()returns how many elements the list holds.
The variable's type is List<String>, and the object is a new ArrayList<>(). Declaring the variable with the interface type is good practice. You can later switch to a different list class without changing the rest of your code.
More Useful Methods
You can also insert at a position, search, and remove.
import java.util.ArrayList;
import java.util.List;
public class TaskList {
public static void main(String[] args) {
List<String> tasks = new ArrayList<>();
tasks.add("wash");
tasks.add("dry");
tasks.add(0, "sort");
System.out.println(tasks); // [sort, wash, dry]
System.out.println(tasks.contains("dry")); // true
System.out.println(tasks.indexOf("wash")); // 1
tasks.remove("wash");
System.out.println(tasks); // [sort, dry]
tasks.remove(0);
System.out.println(tasks); // [dry]
}
}| Method | What it does |
|---|---|
| `add(e)` | Adds `e` to the end |
| `add(i, e)` | Inserts `e` at index `i` and shifts later elements right |
| `get(i)` | Returns the element at index `i` |
| `set(i, e)` | Replaces the element at index `i` |
| `remove(i)` or `remove(e)` | Removes by index or by value |
| `contains(e)` | Returns `true` if `e` is in the list |
| `indexOf(e)` | Returns the first index of `e`, or -1 if `e` is missing |
| `size()` | Returns the number of elements |
Exercise
A list holds [x, y, z]. You call set(0, "w"), then add(1, "q"). What does printing the list show?
Show answer
[w, q, y, z]. set replaces "x" with "w", and add(1, "q") inserts "q" at index 1, which pushes "y" and "z" one place to the right.
Exercise
A list named items has size 4. What is the largest index you can safely pass to get?
Show answer
3. Indexes start at 0, so the last valid index is size() minus 1.