ArrayList
An array's size is fixed. An ArrayList grows and shrinks as you add and remove items.
Java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> tasks = new ArrayList<>();
tasks.add("Learn loops");
tasks.add("Practise arrays");
tasks.add("Build a project");
System.out.println(tasks); // [Learn loops, Practise arrays, Build a project]
System.out.println(tasks.size()); // 3
System.out.println(tasks.get(0)); // Learn loops
tasks.set(1, "Practise ArrayList"); // replace an item
tasks.add(0, "Install Java"); // insert at the front
tasks.remove("Learn loops"); // remove by value
System.out.println(tasks);
System.out.println(tasks.contains("Build a project")); // true
System.out.println(tasks.indexOf("Build a project")); // 2
}
}ArrayList<String>: the type in angle brackets, a generic, says what the list holds. The compiler then refuses to let anything else in.new ArrayList<>(): the empty<>, called the diamond, copies the type from the left.- In real code, declare the variable by its interface,
List<String> tasks = new ArrayList<>();, so the implementation can change later.
Looping
Java
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> scores = new ArrayList<>(List.of(72, 95, 88, 61, 79));
int total = 0;
for (int s : scores) total += s;
System.out.println("Average: " + (double) total / scores.size());
for (int i = 0; i < scores.size(); i++) {
System.out.println((i + 1) + ". " + scores.get(i));
}
scores.removeIf(s -> s < 70); // the -> is a lambda: see the last lesson
System.out.println("70 and over: " + scores);
}
}List.of(...) makes a fixed list; wrapping it in new ArrayList<>(...) gives a copy you can change.
Wrapper types: Integer, not int
A generic type can only hold objects, so a list of numbers is List<Integer>, never List<int>. Java converts between int and Integer automatically, which is called autoboxing. Every primitive has a wrapper class: Integer, Double, Boolean, Character, Long and so on.
Sorting
Java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>(List.of("Mei", "asha", "Leo", "Carlos"));
Collections.sort(names);
System.out.println(names); // capital letters sort before lower case
names.sort(String.CASE_INSENSITIVE_ORDER);
System.out.println(names);
names.sort(Comparator.comparing(String::length));
System.out.println(names); // shortest first
Collections.reverse(names);
System.out.println(names);
System.out.println(Collections.max(List.of(4, 9, 2))); // 9
}
}A list of your own objects
Java
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class Course {
String title;
int lessons;
Course(String title, int lessons) {
this.title = title;
this.lessons = lessons;
}
@Override
public String toString() { return title + " (" + lessons + ")"; }
}
public class Main {
public static void main(String[] args) {
List<Course> courses = new ArrayList<>();
courses.add(new Course("Python", 64));
courses.add(new Course("SQL", 23));
courses.add(new Course("JavaScript", 31));
courses.sort(Comparator.comparingInt(c -> c.lessons));
System.out.println(courses);
courses.sort(Comparator.comparing((Course c) -> c.title).reversed());
System.out.println(courses);
}
}A Comparator describes an ordering without changing the class: here by number of lessons, then by title in reverse.
Finished this lesson?Mark it complete to earn +20 XP and move your progress on.