Next we’ll examine how to use Kotlin’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.
Kotlin has built-in lists. Let’s see how they work! (Open up the documentation as you follow along…)
List
s v. ArraysList
s v. ArraysLike arrays, Kotlin List
s store sequential data.
In Kotlin, the syntax for using List
s is very similar to that for arrays, so it’s easy sometimes to even not be sure what you are working with!
So 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? Just like arrays, that’s pretty easy!
We can access List
contents using bracket notation, just like with arrays:
List
Add and RemoveList
Add and RemoveKotlin List
s provide both an add
and a remove
method, and also support +=
syntax for append.
Let’s explore how they work:
Kotlin provides both mutable and immutable lists:
For now we’ll focus on using mutable lists in our code, as we’ve been doing above. But we’ll come back to this distinction later. For now, just keep this in mind, since it’s easy to forget and create an immutable list and then try to modify it, which doesn’t go well!
Note that the type for a Kotlin List
is List
, but the type of a mutable list is MutableList
.
However, Kotlin will convert a MutableList
to a List
automatically:
Write a method called smallWordFilter
that, given a 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 an Array<String>
.
For example, given the input "Xyz is the very best cat" you would return the Array<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 smallerArray<String>
of the appropriate size, initialized with empty String
sString
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 Kotlin type parameter:
The <String>
on the right tells Kotlin that this list will store String
s.
If we wanted to store Int
s, we’d use <Int>
instead, Double
s, <Double>
, and so on.
Why do lists require a type parameter? It’s so that we can tell Kotlin what we are going to put in them! Once we do, Kotlin 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 Kotlin documentation. Let’s do that together next.
intArray
v. Array<Int>
intArray
v. Array<Int>
Until this point, we’ve been using somewhat different approaches to creating arrays of String
s versus Kotlin basic types, like Int
:
You’ll notice a type parameter in the String
array, but no type parameter in the Int
array, rather a special intArrayOf
method.
It turns out that we can also use arrayOf<Int>
in place of intArray
.
The differences are subtle, but worth a brief discussion:
Don’t worry too much about this at this point.
When you’re working with Int
arrays, we’ll be sure to specify which one to use when it matters.
And in general we’ll begin to use Kotlin lists to solve more problems now, since they are more convenient that arrays.
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 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 need any import
statements, since List
s are built-in to Kotlin and always 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.