Kotlin

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

2019-08-28  本文已影响0人  FindFreeFire

如何在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()
    } 
}
上一篇 下一篇

猜你喜欢

热点阅读