Java Arena
Step 7 of 7

The list toolbox

Here are the list methods from this module and the last one. Three of them are new: add(index, value), isEmpty() and clear().

Method What it does
add(value) Adds the value to the end of the list.
add(index, value) Puts the value at the index. The values from there on move up one place.
get(index) Gives the value at the index.
set(index, value) Replaces the value at the index.
size() Gives the number of values.
remove(index) Removes the value at the index. Later values move down one place.
remove(value) Removes the first copy of the value, if there is one. For numbers: remove(Integer.valueOf(n)).
contains(value) true when the value is in the list.
indexOf(value) The index of the first copy of the value, or -1.
isEmpty() true when the list has no values.
clear() Removes every value.

The methods that take an index crash with an IndexOutOfBoundsException when there's no such index. add(index, value) also accepts size() as the index, which adds to the end.

The three new ones at work:

ArrayList<String> line = new ArrayList<>();
System.out.println(line.isEmpty());
line.add("Bea");
line.add("Carl");
line.add(0, "Abe");
line.add(2, "Bill");
System.out.println(line);
line.clear();
System.out.println(line.isEmpty());
System.out.println(line.size());
Output
true
[Abe, Bea, Bill, Carl]
true
0

isEmpty() says the same as size() == 0, but reads more like what you mean.

A program driven by commands

Many programs are a loop that reads a command, does what it says, and goes round again until the user quits. A list makes a good memory for such a program: the last challenge of this module is a shopping list you run by typing commands.

That's the end of the MOOC's section on lists. Next come arrays: another way to keep many values, with a size that's fixed when the array is made.

Your task

Challenge 1 of 3

Fill in the blanks, so an urgent case goes to the front of the waiting list, and the program then empties the list and checks that it's empty. The program prints:

[Urgent case, Lotta, Sven]
Nobody is waiting

Challenge 2

A phone keeps a list of recently used apps, the most recent first. Write a method markUsed(ArrayList<String> recent, String app) that moves app to the front of the list: if it's already in the list, it's taken out of its old place first, so it's never in the list twice. The check builds a list of maps, mail and camera, calls markUsed with camera and then with notes, and prints the list after each call:

[camera, maps, mail]
[notes, camera, maps, mail]

Challenge 3

Write a shopping list program that reads commands until quit. The commands add and remove are followed by a line with an item.

  • add: add the item and print Added and the item. If it's already on the list, print bread is already on the list (with the item's name) instead.
  • remove: remove the item and print Removed and the item. If it isn't on the list, print cheese is not on the list (with the item's name) instead.
  • show: print each item as - and the item, with a for-each loop, or The list is empty when there's nothing on it.
  • quit: print Bye! and stop.

For the input shown, the program prints:

Added bread
Added apples
bread is already on the list
cheese is not on the list
Removed bread
- apples
Bye!