Kotlin MutableList<Uri> indexOf 返回-1的问题

解决方式

🍎 www.kotlincn.net/docs/refere…

fun main() {
    var position = -1
    val numbers = mutableListOf("one", "two", "three", "four")
    val listIterator = numbers.listIterator()
    while (listIterator.hasNext()){

        position++
        val nextIndex=listIterator.nextIndex()
        val next:String= listIterator.next()
        println("position=$position nextIndex=$nextIndex next=$next")
        
        if(next.equals("two",true)){
            listIterator.remove()
        }   
    }
}

运行结果:

position=0 nextIndex=0 next=one
position=1 nextIndex=1 next=two
position=2 nextIndex=1 next=three
position=3 nextIndex=2 next=four

还是用外部变量的方式间接的从iterator中获取到索引位置吧。-_-||

posted @ 2020-12-16 14:47  javakam  阅读(0)  评论(0)    收藏  举报  来源