Arrays have a fixed length. A List represents an ordered sequence that can offer operations such as add, get and remove. Beginner introduced ArrayList; now separate the type a caller needs from the implementation that stores values. Keep duplicate values when repeated entries mean something.
Work through the example
List<String> means a list whose elements are strings. The angle brackets supply a type argument; Day 4 teaches you to write generic types yourself. ArrayList is an implementation of List. Declaring the variable as List allows its public list operations while avoiding a dependency on an ArrayList-specific detail.
List is an interface: a type describing operations that an implementation provides. ArrayList is a class supplying those operations. Read List<String> tasks = new ArrayList<>(); as “use this ArrayList through the List operations.” You still create an ArrayList object; you cannot construct an interface with new List<>(). The empty <> lets the compiler infer String from the declared type. Day 3 teaches you to write interfaces; using this library interface needs only this distinction.
Printing a list displays its elements in brackets in list order. That is the collection's text representation, not Java array-creation syntax. Lists also compare with equals by checking corresponding elements in order, including repeated values; == still checks whether two references point to the same list object.
Indexes start at zero and must stay below size(). remove(0) removes an index; remove("Read") removes the first equal value. With a List<Integer>, remove(1) still means index 1, not the number 1. Choose the operation deliberately. Integer is the object wrapper for int; Java converts an int when you add it to this list (boxing), and converts an Integer back when the loop requests int (unboxing). A null Integer cannot be unboxed. To remove the value 1 rather than index 1, use remove(Integer.valueOf(1)), as in beginner's number-list example.
Before Run, write the output and identify the exact line responsible for each result. Each editor is a separate program; names and edits in one box do not carry into the next.
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<String> tasks = new ArrayList<>();
tasks.add("Read");
tasks.add("Test");
tasks.add("Read");
tasks.remove("Read");
System.out.println(tasks);
System.out.println(tasks.get(0));
}
}Check your prediction
[Test, Read]
TestCompare the behavior
Do not remove entries directly from a list while traversing it with an enhanced for loop. Build a result list when filtering; the input then stays stable, and “all entries match” is easy to handle. This is intentionally a loop; streams arrive only after you can trace this version. The new list is a different container, even though immutable strings can safely be shared.
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<Integer> values = new ArrayList<>();
values.add(0);
values.add(25);
values.add(-1);
List<Integer> positive = new ArrayList<>();
for (int value : values) {
if (value > 0) {
positive.add(value);
}
}
System.out.println(positive);
System.out.println(values);
}
}Check your prediction
[25]
[0, 25, -1]Name the two different types in the declaration
The declared type tells a caller which operations are available. The constructed object's class supplies the implementation. With List<String> names = new ArrayList<>(), there is one object, not a List object wrapping a second ArrayList. You can pass that reference to a method expecting List without copying its contents.
A method accepting List should rely on its documented operations and mutability contract. Not every List supports add or remove: unmodifiable list factories still produce List references. If your method needs to build a new result, create a known mutable ArrayList locally instead of assuming it may edit an arbitrary caller's list.
Trace a filter with input [Read, Go, Read]. Reading keeps both qualifying Read entries in order. The new result container separates membership, while the immutable String values can be shared. Filtering is a reporting operation here, not a request to delete short entries from the source.
Practice independently
List & ArrayList: practice
Write longTitles(List<String> titles) returning a new list of titles of at least four characters, in original order, preserving duplicates. Inputs and elements are nonnull by contract. Check [Java, Go, Java] returns [Java, Java] and the source still has three entries. Also check an empty list.
Spend a few minutes on your own first. Compile after one small change, read the first error, and check the result against the requirements rather than matching the solution's layout.
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
System.out.println("Implement and call longTitles");
}
}Hint 1: the idea
The result needs order and repeated entries, so use a list rather than a uniqueness structure.
Hint 2: a first step
Create the result before the loop and add only titles passing the length test.
Show one solution
One possible implementation follows. Compare its decisions with yours: which values can change, what inputs are accepted, and what would expose a mistake?
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<String> source = new ArrayList<>();
source.add("Java");
source.add("Go");
source.add("Java");
System.out.println(longTitles(source));
System.out.println(source.size());
System.out.println(longTitles(new ArrayList<String>()));
}
static List<String> longTitles(List<String> titles) {
List<String> result = new ArrayList<>();
for (String title : titles) {
if (title.length() >= 4) {
result.add(title);
}
}
return result;
}
}Expected output:
[Java, Java]
3
[]Change it and test again
Test lengths 3 and 4, an empty input, and a list where every entry matches. Then alter the returned list and confirm that the source list length does not change. Explain why list copying and copying every element are different operations.
Explain before moving on
Why can the variable be a List while the constructed object is an ArrayList? What exact behavior changes if you replace the filter's >= with >?
Completion is saved in this browser only. It does not measure mastery.