17

Let's say I want to iterate through all but the first element in Kotlin IntArray. Currently, I'm doing it like this:

fun minimalExample(nums: IntArray): Unit {
    for(num in nums.sliceArray(IntRange(1,nums.size-1))) println(num)
}

Is there an easy syntax for doing this like in Python (I don't want to have to specify the ending index of the nums array):

for (num in nums[1:])
0

5 Answers 5

11

I think you could use Kotlin's drop which will remove the first n elements of an array.

fun minimalExampleWithDrop(nums: IntArray): Unit {
    for(num in nums.drop(1)) println(num)
}

minimalExampleWithDrop(intArrayOf(1,2,3,4,5,6))
// 2
// 3
// 4
// 5
// 6

Repl.it: https://repl.it/repls/SvelteShadyLivecd

Sign up to request clarification or add additional context in comments.

3 Comments

Drop will change the IntArray into a List<Int>, if you need an IntArray you'll have to convert that back.
It doesn't change the original. It creates a copy of the data in a new list that you can optionally keep a reference to.
10

You can alternatively also use the slice method which is present in lists and arrays. Here are examples for both:

val a = listOf(1, 2, 3, 4)
println(a.slice(1..a.size - 1))

val b = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
println(b.slice(4..5))

This will print out:

[2, 3, 4]
[5, 6]

Comments

4

A basic for loop with 1 as starting index

    val myList = intArrayOf(1,2,3,4,5,6)

    for(i in 1 until myList.size){
        Log.d(TAG,"${myList[i]}")
    }

Or since it's an IntArray you can use it as an Iterator and skip elements like shown here

    val iterator = myList.iterator()
    // skip an element
    if (iterator.hasNext()) {
        iterator.next()
    }
    iterator.forEach {
        Log.d(TAG,"it -> $it")
    }

Comments

2

If the array is very large, you want to avoid making a copy of all its elements, which is what happens if you use drop, slice etc. Instead, asList and subList provide a view into the original array without making any copies:

for(num in nums.asList().subList(1, nums.size))
    println(num)

The Kotlin library documentation doesn't explicitly say that drop and slice create copies, but it explicitly says that asList and subList don't. For example compare these two descriptions:

drop: "Returns a list containing all elements except..."
slice: "Returns a list containing elements at indices in..."
asList: "Returns a List that wraps the original array."
subList: "Returns a view of the portion of this list between..."

You can also click on the "source" links in the above docs to see whether a certain method copies or returns a view of the original.

1 Comment

This is important. Can you provide a link to docs that state that drop, slice etc. make copies?
1

Just to add to @gil.fernandes answer, you can use slice with until like this:

val list = arrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
println(list.slice(0 until 5))

This will output:

[0, 1, 2, 3, 4]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.