Select and arrange the following lines to form code that will first create a list containing the squares of the first 20 numbers and then go through that list and add up all the values, then prints the total.
Section 3.2 The List interface and ArrayList class
You can find the official documentation of these two classes at the respective doc pages for List and ArrayList. If you wonder what the difference between the two is,
1
docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/List.html
2
docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/ArrayList.html
List
describes what operations should be possible, but it is ArrayList
that implements them. We will discuss this important distinction later on, so for now I will focus on the ArrayList
class, but will often simply talk about "lists".A list is simply an object representing a sequence of elements of a specific type, and the ability to perform a rich set of operations. You can create small lists with a special
List.of()
method, or more complex lists via creating an ArrayList
object and adding elements to it. Here’s an example of this:List<String> cats = List.of("Ziggy", "V", "Junior");
cats.get(1); // <--- "V"
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < 20; i++) {
numbers.add(i);
}
// numbers now contains 0, 1, ..., 19
// And now we print them
for (Integer number : numbers) {
System.out.println("This is number: " + number);
}
Notice the angle brackets with a type inside them, like
<String>
. These are called generics and we will come back to them more later, but basically they simply indicate what kind of elements we would be storing in the lists. When you create a list you need to specify that. In our example we have two kinds of lists, one that holds strings and another that holds integers.We also see here an example of a for loop, on lines 4-6. It uses a local variable
i
whose value starts at 0 and increments each time through the loop, until it reaches 20
. For each of those iterations, the body of the for loop adds the number into the list of numbers.We also see a different but very important loop style in lines 9-11. This is usually called a foreach loop, and it is the most common way of looping over a list if you don’t need to modify it and you don’t need to know the index, just the values that are there. Anything that satisfies the Iterable interface allows you to use this loop to access its elements, and you can also use the loop with normal arrays.
Speaking of arrays, you might wonder if Java has arrays and why we haven’t talked about them yet. Yes it does, and in almost every situation you are better off using a list. But in any case, let’s at least look at the syntax:
int[] arr = new int[5]; // array of size 5
for(int i = 0; i < arr.length; i++) {
arr[i] = i * i;
}
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
What are the main differences between arrays and lists?
- Arrays have a fixed size that cannot be changed. Lists typically resize themselves as needed to fit their contents.
- Arrays can hold primitive values, while lists can only hold objects. We will discuss the difference between the two in a later section. In general this is not important enough to warrant using an array.