isaact
isaact

Reputation: 15

Is there a better way to create a mutable list of a given range?

What I'm currently doing:

var idList = mutableListOf<Int>()

fun main() {
    for (number in 1000000..1999999) {
        idList.add(number)
    }

    println(idList[0]) //testing purposes
    println(idList[1]) //testing purposes
}

I feel like this is very inefficient. Is there a better way?

Upvotes: 1

Views: 552

Answers (1)

hakim
hakim

Reputation: 3909

One option I can think of right now is like this

val numbers = (1000000..1999999).toMutableList()
println(numbers.first())
println(numbers.last())

Upvotes: 2

Related Questions