小编Joe*_*han的帖子

如何从Scala中的列表中删除第一个对象?

从Scala中的列表中删除第一个对象的最佳方法是什么?

来自Java,我习惯于使用一种List.remove(Object o)方法从列表中删除第一次出现的元素.既然我在Scala中工作,我希望该方法返回一个新的不可变List而不是改变一个给定的列表.我也可能期望该remove()方法采用谓词而不是对象.总之,我希望找到这样的方法:

/**
 * Removes the first element of the given list that matches the given
 * predicate, if any.  To remove a specific object <code>x</code> from
 * the list, use <code>(_ == x)</code> as the predicate.
 *
 * @param toRemove
 *          a predicate indicating which element to remove
 * @return a new list with the selected object removed, or the same
 *         list if no objects satisfy the given predicate
 */
def …
Run Code Online (Sandbox Code Playgroud)

scala list

4
推荐指数
1
解决办法
3735
查看次数

标签 统计

list ×1

scala ×1