Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Collections, Ranges and Sequences  ›  Lesson

Arrays and Lists

Lists 15 minAn array is a fixed-size block of indexed slots, while a list is an ordered collection that can be read-only or growable; both are indexed from zero.

Arrays and Lists

The café menu has several drinks, and the till records many orders. Storing each in its own variable, drink1, drink2, drink3, falls apart the moment a new drink arrives. Kotlin offers arrays and lists to hold many values under one name.

The rule

Array: fixed slots. List: ordered items. Both count from zero.

Arrays have a size fixed at creation. You can change what is in a slot, but you cannot add or remove slots.

val menu = arrayOf("latte", "mocha", "chai")
menu[1] = "flat white"          // change a slot: allowed
println(menu.size)              // 3
val week = IntArray(7)          // seven zeros, one per day
val doubles = Array(3) { it * 2 }   // 0, 2, 4

IntArray, DoubleArray and friends store plain numbers efficiently. Array(3) { it * 2 } fills each slot by running the braces with it as the index.

Lists are Kotlin's everyday choice. listOf makes a read-only list; mutableListOf makes one you can grow and shrink.

val drinks = listOf("latte", "mocha", "chai")
println(drinks[0])              // latte
println(drinks.first())         // latte
println(drinks.last())          // chai
println(drinks.indexOf("tea"))  // -1: not found
println("chai" in drinks)       // true

val orders = mutableListOf("latte")
orders.add("chai")              // [latte, chai]
orders.removeAt(0)              // [chai]

Reading past the end, drinks[3], throws IndexOutOfBoundsException. drinks.getOrNull(3) returns null instead.

Two differences show up when printing and comparing. println(drinks) shows [latte, mocha, chai], but an array needs contentToString(). And == on two lists compares their items, while == on two arrays asks whether they are the same object; use contentEquals for arrays.

The shortcut

Ask "Will the number of items ever change?" Never, and you care about raw performance or a fixed shape → array. Otherwise → list. For indexes, remember the last index is size - 1.

Reading the third drinkStep 1 of 4
  1. Count from zero

    In listOf("latte", "mocha", "chai"), the indexes are 0, 1 and 2.

Drills

Answer fast — trust the rule.

quick drill

val menu = arrayOf("latte", "mocha"). Which line does not compile?

quick drill

What is listOf("a", "b", "c")[1]?

quick drill

What does IntArray(3) contain?

quick drill

listOf(1, 2) == listOf(1, 2) and arrayOf(1, 2) == arrayOf(1, 2). What are the results?

Remember

  • Arrays have fixed slots; lists are the flexible everyday choice.
  • Indexes run from 0 to size - 1; getOrNull avoids crashes.
  • Print arrays with contentToString() and compare them with contentEquals.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainArray or list for the menu?

The café stores (a) exactly seven daily cup totals, one per weekday, and (b) today's orders, which grow all day. Choose an array or a list for each and explain why. Then explain what orders[5] does when there are only three orders, and a safer way to read it.