Iterating Through Collections

Instead of using a range, we can use the structure of a collection as an iterator. A for loop will iterate through each of the collection’s elements with the loop variable holding the value of the current element. We will focus on lists and sets in this exercise and maps in the next one.

val fruitList = listOf("apples", "oranges", "bananas") for (fruit in fruitList) { println("I have $fruit.") }

In this example, we declare fruit as the loop variable and use fruitList as the iterator. Each iteration of the loop will set fruit to the value of the current element of fruitList. Therefore, the number of elements contained in fruitList determines the number of times println("I have $fruit.") is executed:

I have apples. I have oranges. I have bananas.

When we first learned about lists and sets in the collections lesson, we discussed their commonality as well as their differences. An additional similarity is that the syntax for iterating through a list and a set is the same.

When iterating through a collection, it is often useful to know what element number, or iteration, we are at. To iterate through the indices of a collection you can use its indices property:

val fruitSet = setOf("apples", "oranges", "bananas") for (setIndex in fruitSet.indices) { println("Index = $setIndex") }

Here we see the indices of fruitSet output. Remember that the first index of a list or a set is always 0:

Index = 0 Index = 1 Index = 2

We can also get the index AND the iterator element using the collection’s withIndex() function. In this case we need to destructure the loop variable declaration by declaring two loop variables and enclosing them in parentheses:

val fruitList = listOf("apples", "oranges", "bananas") for ((listIndex, fruit) in fruitList.withIndex()) { println("$listIndex is the index of the element $fruit") }

Using withIndex() and destructuring, we are able to access both the index and the element of fruitList:

0 is the index of the element apples 1 is the index of the element oranges 2 is the index of the element bananas



Now you’ll use collections as iterators. To start, implement a for loop using the list mySchedule. The loop should contain:

  • task as the loop variable.
  • the list mySchedule as the iterator.
  • a println() statement in the loop body that outputs the loop variable.

Great, now look at the set myTasks which is declared with the same elements as the list. We know that myTasks will have fewer elements since some of them are repeated and will only be represented once in the set. Let’s confirm this by printing out the indices AND the elements of the set.

Create another for loop that contains:

  • taskIndex and task as destructured loop variables. Be sure to separate them with a comma and enclose them in parentheses.
  • myTasks using the withIndex() function as the iterator.
  • a println() statement in the loop body with the output taskIndex and task separated by a colon (:). Example output: 0: Eat Breakfast
Folder Icon

Take this course for free

Already have an account?