1. Introduction

When working as developers, we sometimes need to iterate over a collection or array, starting from a specific index position. In such cases, we can use a for loop to achieve this.

In this article, we’ll explore different methods for starting a for loop from a given index in Kotlin.

2. Using the drop() Method

The simplest way to achieve our goal is to use the drop() method. This method accepts an integer n that permits us to skip the first n elements in a list:

@Test
fun `start for loop from given index using the drop method`() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val startIndex = 2

    val result = numbers.drop(startIndex)

    assertEquals(listOf(3, 4, 5), result)
}

In this example, we use the drop() method that returns a new list that contains all elements of the original list except the first two elements as dictated by the value of startIndex.

3. Using the Range Operator

We could also start a for loop from a given index by using the range operator. The range operator in Kotlin is represented by the .. operator:

@Test
fun `start for loop from given index with range operator`() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val startIndex = 2
    val result = mutableListOf<Int>()

    for (i in startIndex..numbers.lastIndex) {
        result.add(numbers[i])
    }

    assertEquals(listOf(3, 4, 5), result)
}

From the code above, we begin iterating a list of numbers from index position 2. We use the range operator to start the for loop from the index position equal to 2 and iterate over the remaining elements in the list. The lastIndex property in the for loop returns the index of the last element in the list.

4. Using the withIndex() Method

Another way to start a for loop from a given index is by using the withIndex() method. This method returns an iterable of indexed values:

@Test
fun `start for loop from given index using withIndex method`() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val startIndex = 2
    val result = mutableListOf<Pair<Int, Int>>()

    for ((index, value) in numbers.withIndex().drop(startIndex)) {
        result.add(index to value)
    }

    assertEquals(listOf(2 to 3, 3 to 4, 4 to 5), result)
}

This code shows how to iterate over a list of numbers starting from an index position of 2. Furthermore, we use the withIndex() method to get an iterable of indexed values from the list. Finally, we use the drop() method to skip over the first two elements as per our starting index position.

From this example, notice that the starting index position of the resulting list isn’t 0. This is because we are applying the drop() method after returning an iterable of indexed list values. If we apply this drop() method before the withIndex() method, we will obtain a list starting from index position 0:

@Test
fun `start for loop from given index using withIndex method after drop() method `() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val startIndex = 2
    val result = mutableListOf<Pair<Int, Int>>()

    for ((index, value) in numbers.drop(startIndex).withIndex()) {
        result.add(index to value)
    }

    assertEquals(listOf(0 to 3, 1 to 4, 2 to 5), result)
}

5. Using the forEachIndexed() Method

Alternatively, we can use the forEachIndexed() method as a way to iterate over a list from a given index. It allows us to loop through elements, along with their indices:

@Test
fun `start for loop from given index using forEachIndexed() method`() {
    val numbers = listOf(1, 2, 3, 4, 5)
    val startIndex = 2
    val result = mutableListOf<Pair<Int, Int>>()

    numbers.drop(startIndex).forEachIndexed { index, value ->
        result.add(index to value)
    }

    assertEquals(listOf(0 to 3, 1 to 4, 2 to 5), result)
}

In this example, we use the drop() method to skip over the first two elements of the list, according to our starting index position. Subsequently, we use the forEachIndexed() method to iterate the remaining list elements along with their indices.

6. Conclusion

In this article, we’ve explored various ways to begin a for loop from a precise index position. To begin with, we looked at using the drop() method to skip over a given number of elements. Then, we looked at other techniques for iterating, such as the range() and withIndex() methods. Finally, we explored the forEachIndexed() method that helps us to iterate over the elements of a collection along with their indices.

As always, the source code is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.