Instruction 3

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

Arrays

Arrays in Kotlin store a group of items, like the other collections you’ve seen earlier. They’re represented by the Array class. The primary differences lie in its properties. Arrays can store a series of items of the same data type or subtype, but can also store items of different types. Also, arrays are are immutable.

fun main() {
  val oceans = arrayOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans.toList()) // To see the items in the Array, you convert it to a List
}
[Pacific Ocean, Southern Ocean, Arctic Ocean, Atlantic Ocean, Indian Ocean]
fun main() {
  val oceans = emptyArray<String>()
  println(oceans)
}
[Ljava.lang.String;@34a245ab
fun main() {
  val books = Array<Int>(3) { 0 }
  println(books.toList())
}
[0, 0, 0]
fun main() {
  val books = Array<Int>(3) { it * 2 }
  println(books.toList())
}
[0, 2, 4]
fun main() {
  val books = Array(3) { index -> index * 2 }
  println(books.toList())
}
[0, 2, 4]
fun main() {
  val oceans = arrayOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans[1]) // Prints "Southern Ocean"
}
fun main() {
  val oceans = arrayOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean")
  println(oceans.toList())
  oceans[3] = "Indian Ocean"
  println(oceans.toList())
}
[Pacific Ocean, Southern Ocean, Arctic Ocean, Atlantic Ocean]
[Pacific Ocean, Southern Ocean, Arctic Ocean, Indian Ocean]

Comparing Arrays

To compare arrays, use contentEquals(). The items will have to be the same in both arrays and at the same position for the two arrays to be equal:

fun main() {
  val oceans1 = arrayOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  val oceans2 = arrayOf("Pacific Ocean", "Southern Ocean", "Arctic Ocean", "Atlantic Ocean", "Indian Ocean")
  println(oceans1.contentEquals(oceans2))
}
true

Using Sequences

Sequences work a bit differently from collections. Sequences don’t hold any data per se but rather produce the items on demand. Sequences in Kotlin are represented by the Sequence interface. First, create an empty sequence with the in-built emptySequence() function:

fun main() {
  val weekdays = emptySequence()
  println(weekdays.toList())
}
Not enough information to infer type variable T
[]
fun main() {
  val weekdaysSequence = sequenceOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  println(weekdaysSequence.toList())
}
[Monday, Tuesday, Wednesday, Thursday, Friday]
fun main() {
  val weekdaysList = listOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
  val weekdaysSequence = weekdaysList.asSequence()
  println(weekdaysSequence.toList())
}
[Monday, Tuesday, Wednesday, Thursday, Friday]
fun main() {
  val evens10to20 = generateSequence(10) { it + 2 }
  println(evens10to20.take(6).count())
  println(evens10to20.take(6).toList())
}
6
[10, 12, 14, 16, 18, 20]
Evaluation stopped while it's taking too long
See forum comments
Download course materials from Github
Previous: Instruction 2 Next: Demo 1