Start learning free

LESSON 145 OF 348 · LOOPING OVER ARRAY INDICES

Looping Through Arrays with Indices in Kotlin

Lesson Read the full explanation

In some instances, we will want to loop through an array using indices.

The indices of an array meals are all the integer values from 0 to meals.size - 1, so we can iterate over them using an index variable i:

val meals = arrayOf("pancakes", "pasta", "pizza", "avocado")

for (i in 0 until meals.size) {
	println("Meal number ${i + 1} is ${meals[i]}.")
}

The output of this code is:

Meal number 1 is pancakes.
Meal number 2 is pasta.
Meal number 3 is pizza.
Meal number 4 is avocado.

Assignment
Now let's print the temperature on each day, in this format:

On day 1 the temperature is 10.

On day 2 the temperature is 60.

...


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    KOTLIN · LOOPING OVER ARRAY INDICES

    Looping Through Arrays With Indices in Kotlin, explained

    What this lesson covers

    A for (x in array) loop hands you each element but not its position. Sometimes you need both: to number the items, to compare neighbours, or to write into an array. Then you loop over the indices of the array and read each element with array[i].

    The indices of an array start at 0 and end at size - 1. An array with four elements has the indices 0, 1, 2 and 3. A loop over exactly those values visits every element once.

    The syntax

    The range 0 until n contains the integers from 0 up to n - 1. The upper end is excluded, which is exactly the set of valid indices when n is the size:

    val meals = arrayOf("pancakes", "pasta", "pizza", "avocado")
    for (i in 0 until meals.size) {
        println("Meal number ${i + 1} is ${meals[i]}.")
    }
    

    This prints:

    Meal number 1 is pancakes.
    Meal number 2 is pasta.
    Meal number 3 is pizza.
    Meal number 4 is avocado.
    

    Three pieces are worth separating:

    • for (i in 0 until meals.size). Kotlin declares i for you, with no var or type, and gives it the next value of the range on each pass.
    • meals[i]. Square brackets read the element at index i.
    • ${i + 1}. A string template inserts the value of any expression written between ${ and }. The index is 0-based and people count from 1, so the printed number is i + 1. For a plain variable, $name without braces is enough.

    The exercise

    The lesson gives you an array of daily temperatures and asks you to print one line per day in the form On day 1 the temperature is 10., with the day starting at 1. The code uses the same two ideas as the example above.

    1. Loop over the indices. for (i in 0 until temperatures.size) makes i take the values 0, 1, 2 and 3 for this four-element array.
    2. Print with a template. Inside the loop, println("On day ${i + 1} the temperature is ${temperatures[i]}.") builds the line. The day is i + 1 because days start at 1, and the temperature is temperatures[i] because arrays start at 0.
    val temperatures = intArrayOf(10, 60, -5, 32)
    for (i in 0 until temperatures.size) {
        println("On day ${i + 1} the temperature is ${temperatures[i]}.")
    }
    

    It prints:

    On day 1 the temperature is 10.
    On day 2 the temperature is 60.
    On day 3 the temperature is -5.
    On day 4 the temperature is 32.
    

    The negative number needs no special care. The template converts the Int to text, with its minus sign, and the closing period comes from the template text itself.

    Other ways to get the indices

    • temperatures.indices. It is the range 0..size - 1 made for you, so for (i in temperatures.indices) visits the same values as 0 until temperatures.size and is harder to get wrong.
    • withIndex(). It pairs each element with its position, and a destructuring declaration unpacks the pair: for ((i, t) in temperatures.withIndex()). Here i is the index and t the temperature, so no temperatures[i] is needed.

    The lesson uses until with size because it shows exactly where the bound comes from.

    Common mistakes

    • Going one past the end. With 0..temperatures.size, the range is inclusive, so the last value of i is 4, and temperatures[4] throws ArrayIndexOutOfBoundsException with the message Index 4 out of bounds for length 4. Use until for an excluded end.
    • Using 0..size - 1 carelessly. It is valid, since the range is inclusive and ends at the last index. For an empty array it becomes 0..-1, which is an empty range, so it works too. The until form avoids the subtraction.
    • Printing the index as the day. Without + 1, the output starts at "On day 0".
    • Forgetting the braces in a template. "$temperatures[i]" inserts only the variable temperatures (printed as an unreadable array reference), followed by the literal text [i]. Anything more than a name needs ${...}.
    • Typing the trailing period wrong. The line ends with a period after the temperature, and the exact text matters to the lesson's tests.

    Key takeaways

    • Array indices run from 0 to size - 1.
    • 0 until n excludes n and 0..n includes it. For array indices, use until with size, or use .indices.
    • Read an element with array[i].
    • Insert expressions in strings with ${...}.
    • Use an index loop when you need the position, and a plain for (x in array) when you don't.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free