mohammad saberi
mohammad saberi

Reputation: 1

why mutableSet does not update !! for item in list in Kotlin

i want have setList of 1 to 10 but "currentNumber" (an item of list) can not toNext!!

var numList = mutableSetOf(1)
for (currentNumber in numList) {

    var temp = currentNumber
    temp++

    numList.add(temp)
    if (currentNumber == 10)
        break
}

println(" final List 0 to 10 $numList")

Upvotes: 0

Views: 375

Answers (1)

Maroš Šeleng
Maroš Šeleng

Reputation: 1640

It is because you are modifying the set whilst iterating through it.

You can create a sequence of number using the rangeTo operator as follows:

1..100 creates an IntRange which you can convert to list or set.

Upvotes: 1

Related Questions