val CHUNK_SIZE = 20
val vehicles = (0..177).map { "Car ${it}" }.toMutableList()
val carListIterator = vehicles.iterator()
val removalChunk = mutableListOf<String>()
while(carListIterator.hasNext()) {
removalChunk.add(carListIterator.next())
carListIterator.remove() // this removes the element that was just returned
if(removalChunk.size >= CHUNK_SIZE || !carListIterator.hasNext()) {
//store chunk elsewhere
println("Storing: ${removalChunk.joinToString()}")
removalChunk.clear()
}
}
1条答案
按热度按时间v6ylcynt1#
你不必说你是否关心你想要以什么顺序删除列表项。如果你不关心,那么下面的代码展示了你如何实现它。注意,原始列表必须是 * 可变的 *,
remove()
操作才能起作用