JAVA · ОСНОВЫ · LESSON 23

The list that grows

ArrayList · about 16 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

Massive and growing list

The size of the usual array is set in advance. ArrayList is convenient when you want to add items later. Connect it to import.

import java.util.ArrayList;
ArrayList<String> pets = new ArrayList<>();

✦ There will be a list of words.

STEP 2

Add and count.

add puts the item at the end. size() says how many items are on the list now.

pets.add("кот");
pets.add("пёс");
System.out.println(pets.size());
Growing list
Empty.0add кот1add пёс2

✦ The result is 2.

IN-CLASS PRACTICE

Type the code yourself

Type an example manually. Check the signs and brackets.

import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        ArrayList<String> pets = new ArrayList<>();
        pets.add("кот");
        pets.add("пёс");
        System.out.println(pets.size());
    }
}
Open the editor for class work
HOMEWORK

Solve it yourself

Подключи ArrayList. Создай ArrayList<String> colors, добавь «красный», «синий», «зелёный» и выведи размер списка через size().

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework