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

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

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


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


正确代码如下

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

你可能感兴趣的:(如何在kotlin中安全的遍历删除List/数组中不需要的数据)