LinkedList:迭代并删除元素

时间:2013-04-02 10:48:38

标签: scala iterator iteration

在Scala中,在迭代LinkedList的元素时,我希望有一些方法remove()删除当前元素,(非常重要)使迭代器指向下一个元素(或者如果是当前元素是最后一个;如果没有更多元素,则为null或其他东西。)

1 个答案:

答案 0 :(得分:0)

不确定这是否是您想要的但是如何:

@annotation.tailrec
def round(xs: Set[(Int, Int)]) = {
    // here you might check if items are exhausted and possibly don't go through recursion deeper


    val suitable = xs.filter {
        case (x, i) => 
            println("Element "+x+" on position "+i)
            x != 0
    }

    // do something with suitable items

    round(xs.diff(suitable)) // next round with items that doesn't succeed in current round
}

val list = List(3,4,5)
round(list.zipWithIndex.toSet)