Java Arena
Step 2 of 8

What a list holds

The type between < and > says what goes into the list. A list of whole numbers is an ArrayList<Integer>, a list of decimal numbers an ArrayList<Double>, and a list of true and false values an ArrayList<Boolean>:

ArrayList<Integer> scores = new ArrayList<>();
scores.add(12);
scores.add(7);
scores.add(12);
System.out.println(scores);

ArrayList<Double> prices = new ArrayList<>();
prices.add(4.5);
prices.add(3.0);
System.out.println(prices);
Output
[12, 7, 12]
[4.5, 3.0]

Only values of that type fit: scores.add("twelve") doesn't compile, because "twelve" is a String, not a whole number.

Integer, not int

Why Integer and not int? A list can only hold objects, and int, double and boolean aren't objects: they're primitive types, plain values built into the language. Each of them has an object partner, written with a capital letter: Integer for int, Double for double and Boolean for boolean. A primitive type in a list doesn't compile:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<int> scores = new ArrayList<>();
        scores.add(12);
        System.out.println(scores);
    }
}
What javac prints
Main.java:5: error: unexpected type
        ArrayList<int> scores = new ArrayList<>();
                  ^
  required: reference
  found:    int
1 error

required: reference means javac wants an object type there. Write ArrayList<Integer> instead.

Otherwise you rarely notice the difference, because Java converts between the two by itself. scores.add(12) turns the int 12 into an Integer, and a value you take out of the list can go straight into an int variable (the next step shows how).

One catch: Java turns an int into an Integer, but never into a Double. In an ArrayList<Double>, write whole numbers with a decimal point: prices.add(3.0) works, prices.add(3) doesn't compile.

Strings are objects already, so a list of text is simply an ArrayList<String>.

Your task

Challenge 1 of 3

Fill in the two list types so the program compiles and prints:

[14, 3]
[true, false]

Challenge 2

Read how many days of rain were measured, and then each day's rainfall in millimeters, as a decimal number, one per line. Add each day's rainfall to an ArrayList<Double>, and print Rainfall: followed by the list. For the input 3, 2.5, 4 and 0.75 the program prints:

Rainfall: [2.5, 4.0, 0.75]

Challenge 3

This program doesn't compile: javac finds two errors, one for each list. Fix them so the program prints:

[31, 8]
[1.82, 1.0]