LinkedList:迭代并删除元素 [英] LinkedList: Iterate and remove element

查看:213
本文介绍了LinkedList:迭代并删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Scala, while iterating through the elements of a LinkedList, I would like to have some method remove() which removes the current element and (very important) makes the iterator point to the next element (or to the first if the current element is the last one; to null or something if there are no more elements).

推荐答案

不确定这是不是你的意思想但是怎么样:

Not sure if this what you want but how about:

@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)

这篇关于LinkedList:迭代并删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆