Java Arena
Step 1 of 9

Lists, a quick review

Part 6 starts with a tool you've used since module 13, the list, and puts it somewhere new: inside an object. First, a quick look back at what you already know.

A list of Strings

ArrayList<String> stops = new ArrayList<>();
stops.add("Harbor");
stops.add("Market");
stops.add("Old Town");
System.out.println(stops.size() + " stops, the second is " + stops.get(1));
for (String stop : stops) {
    System.out.println("Next: " + stop);
}
Output
3 stops, the second is Market
Next: Harbor
Next: Market
Next: Old Town
  • ArrayList<String> is a list of Strings, and new ArrayList<>() makes a new, empty one.
  • add puts a value at the end, size() tells how many values there are, and get(1) gives the value at index 1. Indexes start at 0.
  • The for-each loop goes through the values in order.

Module 14 added more: contains, indexOf, remove, isEmpty and clear. A list of whole numbers is an ArrayList<Integer>.

A list of objects

Module 19 put objects of your own classes on a list. The loop then gets one object at a time, and asks it for its values with its getters:

Main.java
import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<Book> books = new ArrayList<>();
        books.add(new Book("Emma", 474));
        books.add(new Book("Dracula", 418));
        books.add(new Book("Kindred", 264));

        int pages = 0;
        for (Book book : books) {
            System.out.println(book);
            pages = pages + book.getPages();
        }
        System.out.println("In all: " + pages + " pages");
    }
}
Book.java
public class Book {
    private String title;
    private int pages;

    public Book(String title, int pages) {
        this.title = title;
        this.pages = pages;
    }

    public String getTitle() {
        return this.title;
    }

    public int getPages() {
        return this.pages;
    }

    public String toString() {
        return this.title + ", " + this.pages + " pages";
    }
}
Output
Emma, 474 pages
Dracula, 418 pages
Kindred, 264 pages
In all: 1156 pages

Whose list is it?

In these programs, the list is a local variable of main. For one list, that's fine. Now picture a home with two bookshelves, one in the living room and one in the bedroom. main would need two lists, and two more variables for the rooms' names, and every method would take a list as a parameter: printBooks(bedroom), totalPages(livingRoom). Nothing ties a room's name to its list, and any line of the program could change any list.

Module 21 had the answer to this kind of mess: an object looks after its own data. A list is data too, so it can be an instance variable of an object:

public class Bookshelf {
    private String room;
    private ArrayList<Book> books;

    // a constructor, and methods that add books, print them, count them...
}

Each Bookshelf then keeps its own room and its own books, and main only calls methods such as shelf.add(book). The rest of this module builds classes like this one, a piece at a time. First, a warm-up with lists as you know them.

Your task

Challenge 1 of 3

Runner.java is ready: a runner has a name and a time in minutes, with the getters getName() and getMinutes(), and a toString that gives, for example, Ada, 27 min. In Main.java, write the method printFaster(ArrayList<Runner> runners, int limit). It prints every runner whose time is less than limit minutes, one per line, in the order of the list. The check runs:

ArrayList<Runner> runners = new ArrayList<>();
runners.add(new Runner("Ada", 27));
runners.add(new Runner("Bo", 35));
runners.add(new Runner("Cy", 22));
printFaster(runners, 30);

and expects:

Ada, 27 min
Cy, 22 min

Challenge 2

What does this program print? A list is an object, so a list variable holds a reference to it, just like a variable of your own classes (module 22).

Challenge 3

Read runners until an empty line. Each line has a name and a time in minutes, separated by a space, like Ada 27. Make a Runner of each line and add it to the list runners. Runner.java is the class from the first challenge.

Then print how many runners there are, like Runners: 3, and after that every runner, starting from the last one read and ending with the first. For the input shown below, the program prints:

Runners: 3
Cy, 22 min
Bo, 35 min
Ada, 27 min