While the first range will hold the list of multipliers corresponding to different rows, on the other hand, the second range will hold the result values. Further, we’ll need two ranges in our loop. 5 Ways to Iterate Over a List in Kotlin Using forEach () method Using for loop An alternative for cycle utilizes the size of the list Using forEachIndexed () method Using a ListIterator and a while loop Here is a complete source code to demonstrate five ways of looping over a list in Kotlin.
We will be using a list, but it also works with an array. Let’s advance our understanding of looping with multiple variables by applying concepts of ranges and the zip operator to generate multiplication tables, where each row shows up in the format: factor x multiplier = resultĪs the factor will remain the same in all the rows, we’ll have to loop through two multiplier and result variables. First, lets see how to drop the first n items in a collection.