Kotlin Lec 5 2020
Kotlin Lec 5 2020
Программирование на Kotlin
(p.5 – Arrays & Lists)
• Since the array only contains integers, Kotlin infers the type of
evenNumbers to be an array of Int values.
• This type is written as Array<Int>. The type inside the angle
brackets defines the type of values the array can store, which
the compiler will enforce when you add elements to the array.
Creating arrays
• It’s also possible to create an array with all of
its values set to a default value:
val fiveFives = Array(5) { 5 } // 5, 5, 5, 5, 5
print(players.isEmpty())
// > false
The list isn’t empty, but you need at least two players to start a
game. You can get the number of players using the size property:
if (players.size < 2) {
println("We need at least two players!")
} else {
println("Let's start!")
}
// > Let's start!
Using properties and methods
• How would you get the first player’s name?
• Lists provide the first() method to fetch the first object of a
list:
var currentPlayer = players.first()
println(currentPlayer) // > Alice
players.slice(1..3).contains("Alice") // false
Modifying lists
• You can make all kinds of changes to mutable
lists, such as adding and removing elements,
updating existing values, and moving elements
around into a different order.
• Now we’ll see how to work with the list to match
up with what’s going on in your game.
Appending elements
• If new players want to join the game, they need
to sign up and add their names to the list.
• Eli is the first player to join the existing four
players. You can add Eli to the end of the array
using the add() method:
players.add("Eli")
This method does two things: It removes the element and then
returns a Boolean indicating whether the removal was successful,
so that you can make sure the cheater has been removed!
Removing elements
• To remove Cindy from the game, you need to
know the exact index where her name is stored.
• Looking at the list of players, you see that she’s
third in the list, so her index is 2.
• You can remove Cindy using removeAt()
val removedPlayer = players.removeAt(2)
println("$removedPlayer was removed")
// > Cindy was removed
This code replaces the player Eli with the player Alice. You then
call sort() on the list to make sure the list remains sorted in
alphabetical order.
Iterating through a list
• Before the players leave, you want to print the
names of those still in the game.
• Like for arrays, you can do this using the for loop
for (player in players) {
println(player)
}
// > Alice
// > Anna
// > Bob
// > Dan
// > Franklin
Iterating through a list
• If you need the index of each element, you can
iterate over the return value of the list’s
withIndex() method, which can be destructed to
each element’s index and value:
for ((index, player) in players.withIndex()) {
println("${index + 1}. $player")
}
// > 1. Alice
// > 2. Anna
// > 3. Bob
// > 4. Dan
// > 5. Franklin
Iterating through a list
• It’s getting late, so the players decide to stop for
the night and continue tomorrow.
• In the meantime, you’ll keep their scores in a
separate list.
val scores = listOf(2, 2, 8, 6, 1)
Iterating through a list
val scores = listOf(2, 2, 8, 6, 1)
You could use this function to calculate the sum of the players’ scores:
println(sumOfElements(scores)) // > 19
Let’s code!
Questions?
Algorithms & Programming
Программирование на Kotlin
(p.5 – Arrays & Lists)