Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Lists: List and ArrayList

An array has a fixed length: create it for five elements and you cannot add a sixth. A list (List) grows and shrinks by itself. It is the most common collection in Java programs.

Creating a list

The most widely used list implementation is ArrayList. The element type is written in angle brackets: List<String> is a list of strings:

void main() {
    List<String> names = new ArrayList<>();
    names.add("Anna");
    names.add("Oleg");
    IO.println(names);
    IO.println(names.size());
}

Output:

[Anna, Oleg]
2

Empty brackets <> on the right are enough — Java takes the type from the left side. The size() method returns the number of elements.

Reading and replacing

Elements are numbered from zero, as in an array. The get() method reads an element by index, and set() replaces it:

void main() {
    List<String> fruits = new ArrayList<>();
    fruits.add("apple");
    fruits.add("pear");
    fruits.add("plum");
 
    IO.println(fruits.get(0));
    fruits.set(1, "peach");
    IO.println(fruits);
}

Output:

apple
[apple, peach, plum]

Inserting, removing and searching

add(index, element) inserts an element in the middle and shifts the rest. remove() deletes, contains() checks presence, and indexOf() returns the position or -1:

void main() {
    List<String> tasks = new ArrayList<>();
    tasks.add("buy bread");
    tasks.add("call mum");
    tasks.add(0, "wake up");
 
    tasks.remove("buy bread");
    IO.println(tasks);
    IO.println(tasks.contains("wake up"));
    IO.println(tasks.indexOf("call mum"));
}

Output:

[wake up, call mum]
true
1

An unmodifiable list

List.of() creates a list with elements right away. Such a list is unmodifiable: you cannot add, remove or replace an element. This is handy for data that must not change:

void main() {
    List<String> days = List.of("Mon", "Tue", "Wed");
    IO.println(days);
    days.add("Thu");
}

Output:

[Mon, Tue, Wed]
Exception in thread "main" java.lang.UnsupportedOperationException

If you need a modifiable list with initial data, wrap it: new ArrayList<>(List.of("Mon", "Tue")).

Lists of numbers

Collections store only objects, so instead of int you write the wrapper class Integer, instead of double — Double. Java turns a number into an object and back by itself — this is called autoboxing:

void main() {
    List<Integer> scores = new ArrayList<>(List.of(5, 3, 8));
    int total = 0;
    for (int score : scores) {
        total += score;
    }
    IO.println("Sum: " + total);
}

Output:

Sum: 16

How wrappers work and why they must not be compared with == is covered in the wrapper classes lesson.

Common mistake

A list has two remove methods: remove(int index) removes by index, and remove(Object o) by value. For a list of Integer the number in parentheses is treated as an index:

void main() {
    List<Integer> numbers = new ArrayList<>(List.of(10, 20, 30));
    numbers.remove(1);
    IO.println(numbers);
    numbers.remove(Integer.valueOf(10));
    IO.println(numbers);
}

Output:

[10, 30]
[30]

remove(1) removed the second element, not the number one. To remove by value, pass an object: Integer.valueOf(10).

Thanks for reading!

Donate to the site — we work hard for you.