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…)
List
Creation and InitializationList
Creation and InitializationLike arrays, Java List
s 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 Lists
s.
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 List
.
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 List
s, and requires a separate import
statement:
List
Get and SetList
Get and SetNow that we have a List
with a few values, how do we access them and modify them?
With arrays we used bracket notation, but List
s work differently:
List
Add and RemoveList
Add and RemoveNow we get to something that arrays can’t do—modify their length!
Java List
s provide both an add
and a remove
method.
Let’s explore how they work:
Before we go on, a somewhat dull but important digression.
In the examples above we’ve seen how to create List
of String
s:
But how would we create a list of int
values?
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, int
(and long
and boolean
, etc.) are primitive types.
So we can’t store them in List
s.
Uh-oh.
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:
byte
-> Byte
short
-> Short
int
-> Integer
long
-> Long
float
-> Float
double
-> Double
char
-> Character
boolean
-> Boolean
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 null
:
Write a method called smallWordFilter
that, given a non-null
String
containing words separated by single spaces
(" "
), returns all the words in the original String
that are 3 characters or shorter in the same order in
which they appeared in the original String
, as a String[]
.
For example, given the input "Xyz is the very best cat" you would return the String[]
{"Xyz", "is", "the",
"cat"}
.
We have skipped both "very" and "best" because they are longer than 3 characters.
This is a problem that would be much easier to solve using a list, since you don't know exactly how many part of
the input String
are 3 characters or smaller!
But this can be done with an array, if somewhat awkwardly.
Here's a solution sketch to help you get started:
" "
String
that are 3 characters or smallerString[]
of the appropriate sizeString
parts filling your output array as you goWe've provided some starter code to help you get going on this problem.
The syntax that we introduce above is our first example of a Java type parameter:
The <String>
on the left tells Java that this List
variable will store String
s.
And the <String>
on the right tells Java to create a new ArrayList
that will store String
s.
If we wanted to store int
s, 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 in a later lesson.
It’s important to understand how to identify type parameters when examining Java documentation. Let’s do that together next.
List
v. ArrayList
List
v. ArrayList
In the examples above we’ve used two imports: List
and ArrayList
:
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.
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 List
.
However, Java will let you create an untyped List
, and this is what will happen if you omit the <String>
or <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.
Write a method called smallWordFilter
that, given a non-null
String
containing words separated a single space
(" "), returns all the words in the original String
that are 3 characters or shorter in the same order in
which they appeared in the original String
, as a List<String>
.
For example, given the input "Xyz is the very best cat" you would return the List<String>
containing {"Xyz",
"is",
"the",
"cat"}
.
We have skipped both "very" and "best" because they are longer than 3 characters.
Note that you should not add any import
statements, since both java.util.List
and java.util.ArrayList
are
already available.
Having a technical background isn’t a requirement for helping drive positive change in technology. Illini Reshma Saujani earned undergraduate degrees in Political Science and Speech Communication here at the University of Illinois, and graduate degrees from Harvard’s Kennedy School and Yale Law School. While a practicing lawyer and political aspirant, Saujani noticed the gender disparity in high school computer science classrooms, which caused her to found Girls Who Code in 2012 to encourage women to pursue technical careers.
She’s also spoken and written about female empowerment in the workplace. Her latest book, “Pay Up” was informed by her experience during the pandemic, and identifies structural forces in the American workplace that are harming women:
Need more practice? Head over to the practice page.