Instruction 1

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Collections

A collection in Kotlin is a group of items. Think of situations as a programmer where you’d use collections. These collections could be items from the grocery store or letters of the alphabet. Collections could also be countries on a continent or members of a family. Visit Kotlin Playground to start a new session.

fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans)
}
[Pacific Ocean, Southern Ocean, Arctic Ocean, Atlantic Ocean, Indian Ocean]

Using Lists

A List stores items in the order in which they’re added or created. This is a useful property to keep in mind. It’s not the same for every type of collection. You’ll see an ordering difference in the other types of collections.

[Pacific Ocean, Southern Ocean, Arctic Ocean, Atlantic Ocean, Indian Ocean, Indian Ocean, Indian Ocean]

Indexing

You access the elements using a zero-based index. This means that the first item in the list has an index of 0. This is an important property of collections. In Kotlin, as well as most other programming languages, counting begins at 0 instead of 1.

fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans[0]) // Prints "Pacific Ocean"
}
[Pacific Ocean]

Implementing Mutable Lists

Now that you’re able to reference items in a List at specific positions, what can you do with these elements? You can remove the element if the collection is mutable - editable or changeable.

fun main() {
  val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans)
}
fun main() {
  val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
  println(oceans)
  oceans.add("Indian Ocean")
  println(oceans)
}
fun main() {
  val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
  oceans.removeAt(2)
  println(oceans)
}
[Pacific Ocean, Southern Ocean]

Immutable Lists

Immutable collections may be assigned to a mutable or an immutable variable. A collection’s mutability is different from the mutability of the variable it’s assigned to. The immutability of a list applies to the elements in the collection and not the variable or object itself. So if an immutable collection is assigned a var, the variable or object can be updated in the course of the program. And if an immutable collection is assigned a val, it can’t be updated:

fun main() {
  var oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
  println(oceans)
  oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans)
}
fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
  println(oceans)
  oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans)
}
Val cannot be reassigned
fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
  println(oceans)
  oceans.add("Indian Ocean") // Not possible
  println(oceans)
}
Unresolved reference: add
fun main() {
  val oceans = mutableListOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean")
  println(oceans)
  oceans.add("Indian Ocean")
  println(oceans)
}
fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans)
  println("There are ${oceans.size} oceans in the world.")
}
fun main() {
  val oceans = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  for (ocean in oceans) {
      println(ocean)
  }
}

Comparing Lists

To compare two lists, use the equality operator ==. For two lists to be equal, they must have the same data type, content, number of items, and be in the same order.

fun main() {
  val oceans1 = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  val oceans2 = listOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans1 == oceans2) // Prints true
}
See forum comments
Download course materials from Github
Previous: Introduction Next: Instruction 2