美文网首页Kotlin
如何在kotlin中安全的遍历删除List/数组中不需要的数据

如何在kotlin中安全的遍历删除List/数组中不需要的数据

作者: FindFreeFire | 来源:发表于2019-08-28 17:59 被阅读0次

如何在kotlin中安全的删除List/数组中不需要的数据

如果使用了foreach 和 for in 来对list进行遍历删除,会出现IndexOutOfBoundException的错误


使用 iterator 来对列表进行遍历,进行数据的删除


正确代码如下

val dataList = ArrayList<DataInfo>()
val mIterator = dataList.iterator()
while (mIterator.hasNext()) {
    val next = mIterator.next()
    if (next.isDetele) {
       mIterator.remove()
    } 
}

相关文章

网友评论

    本文标题:如何在kotlin中安全的遍历删除List/数组中不需要的数据

    本文链接:https://www.haomeiwen.com/subject/eaarsctx.html