Next we'll examine how to use Java's lists. We've stored sequential data before in an array, but lists are more flexible and more suited to certain tasks.
Like arrays, lists are an ordered data structure. However, unlike arrays, the size of a list can change as the program runs. This makes them much more appropriate for solving certain problems.
Java has built-in lists. Let's see how they work! (Feel free to open up the documentation as you follow along...)
ListCreation and Initialization
Like arrays, Java
Lists store sequential data.
However, the syntax for using them is a bit different.
Let's review the array operations we're familiar with and show how they map onto
First, let's show how we create an array and
List, and check their size:
Note that with the
List, unlike the array we do not need to specify a size when we create the
This is because the size of a
List can change!
Next up, how do we initialize a list with a set of values and print its contents?
This turns out to be a bit harder than you would like with Java
Lists, and requires a separate
ListGet and Set
Now that we have a
List with a few values, how do we access them and modify them?
With arrays we used bracket notation, but
Lists work differently:
ListAdd and Remove
Now we get to something that arrays can't do—modify their length!
Lists provide both an
add and a
Let's explore how they work:
import java.util.List;import java.util.ArrayList;import java.util.Arrays;List<String> list = new ArrayList<>(Arrays.asList("1", "2", "4"));System.out.println(list);
Before we go on, a somewhat dull but important digression.
In the examples above we've seen how to create
But how would we create a list of
This doesn't seem to work:
The problem here is that Java lists have to store objects, and if you remember from early in the course,
boolean, etc.) are primitive types.
So we can't store them in
Happily, there's a simple solution. For each primitive type Java provides a boxing class that is a object but can store an instance of a primitive type. The boxing classes have capitalized names that are usually the same as the primitive type, with two exceptions:
Thrilling, I know. But, happily, this now works:
Under certain conditions Java will automatically box (primitive to object) and unbox (object to primitive) values for you:
But not always. In particular, arrays are not automatically boxed:
Unlike their primitive types, boxing types can also be
The syntax that we introduce above is our first example of a Java type parameter:
<String> on the left tells Java that this
List variable will store
<String> on the right tells Java to create a new
ArrayList that will store
If we wanted to store
ints, we'd use
<Integer> instead, for reasons that we explain below.
Usually these two types are the same, and so we can use the diamond operator for convenience:
It allows us to omit the
String on the right side of this common expression, and saves us a few keystrokes.
Why do lists require a type parameter? It's so that we can tell Java what we are going to put in them! Once we do, Java will help us avoid common mistakes:
These mistakes are also caught before you program is run, at a step called compilation that we'll explore later this week.
It's important to understand how to identify type parameters when examining Java documentation. Let's do that together next.
In the examples above we've used two imports:
This may seem a bit mysterious at the moment.
And we won't be able to fully explain this until a bit later.
But there is a difference between the two, and a good reason that we use both and not just
ArrayList, which some of you may have seen in previous courses.
We'll return to this topic later this semester.
We will almost always be using type parameters with our lists!
This help make them safer, since we are telling Java what we will put in and take out of the
However, Java will let you create an untyped
List, and this is what will happen if you omit the
<Integer> type parameter.
Let's discuss some of the problems that this can cause:
Overall we will avoid using bare or untyped lists and always provide type parameters when we use a
List in our code.
Need more practice? Head over to the practice page.