如何从向量中删除元素? [英] How do I remove an element from a Vector?

查看:221
本文介绍了如何从向量中删除元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我现在正在做的:

  private var accounts = Vector.empty [Account] 

def removeAccount(account:Account)
{
accounts = accounts.filterNot(_ == account)
}

有更可读的解决方案吗?理想情况下,我想写 accounts = accounts.remove(account)

解决方案

我会用这个:

  accounts filterNot account == 

这对我来说很好,但是ymmv。我也想要一个计数不采取谓词,但集合库真的缺乏专门的方法,其中一个谓词可以推广操作。 p>

直到2.8.x, - 方法,因为语义问题。它可以真的回来了2.10如果我的记忆正在为我服务,但它没有。 修改:我检查了它,发现 - 现在保留为可修改应用。我会赞成 - : / : - 虽然在序列上,第一个或最后一个元素等于某事。任何人都愿意为此买票?我会upvote它。 : - )


This is what I'm doing now:

  private var accounts = Vector.empty[Account]

  def removeAccount(account: Account)
  {
    accounts = accounts.filterNot(_ == account)
  }

Is there a more readable solution? Ideally, I'd like to write accounts = accounts.remove(account).

解决方案

I'd use this:

accounts filterNot account.==

Which reads pretty well to me, but ymmv. I'd also like a count that doesn't take a predicate, but the collection library is really lacking in specialized methods where one with a predicate can generalize the operation.

Until 2.8.x, there was a - method, which got deprecated, iirc, because of semantic issues. It could actually have come back on 2.10 if my memory is serving me right, but it didn't. Edit: I checked it out, and saw that - is now reserved for a mutable method that modifies the collection it is applied on. I'd be all in favor of -:/:- though on sequences, where it makes sense to drop the first or last element equal to something. Anyone willing to front a ticket for that? I'd upvote it. :-)

这篇关于如何从向量中删除元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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