从 Scala 中的字符串数组中删除第 n 个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21057080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing nth element from a String Array in Scala
提问by nfc-uk
Assuming I have a (what I assume is mutable by default) Array[String]
假设我有一个(我认为默认情况下是可变的)Array[String]
How in Scala can I simply remove the nth element?
如何在 Scala 中简单地删除第 n 个元素?
No simple method seems to be available.
似乎没有简单的方法可用。
Want Something like (I made this up):
想要类似的东西(我编的):
def dropEle(n: Int): Array[T]
Selects all elements except the nth one.
n
the subscript of the element to drop from this Array.
Returns an Array consisting of all elements of this Array except the
nth element, or else the complete Array, if this Array has less than
n elements.
Many thanks.
非常感谢。
回答by som-snytt
That is what views are for.
这就是视图的用途。
scala> implicit class Foo[T](as: Array[T]) {
| def dropping(i: Int) = as.view.take(i) ++ as.view.drop(i+1)
| }
defined class Foo
scala> (1 to 10 toArray) dropping 3
warning: there were 1 feature warning(s); re-run with -feature for details
res9: scala.collection.SeqView[Int,Array[Int]] = SeqViewSA(...)
scala> .toList
res10: List[Int] = List(1, 2, 3, 5, 6, 7, 8, 9, 10)
回答by swartzrock
The problem is with the semi-mutable collection you chose, since an Array's elements may be mutated but its size cannot be changed. You really want a Buffer which already provides a "remove(index)" method.
问题在于您选择的半可变集合,因为 Array 的元素可能会发生变异,但其大小无法更改。你真的想要一个已经提供“remove(index)”方法的缓冲区。
Assuming you already have an Array, you can easily convert it to and from a Buffer in order to perform this operation
假设您已经有一个 Array,您可以轻松地将其与 Buffer 进行转换以执行此操作
def remove(a: Array[String], i: index): Array[String] = {
val b = a.toBuffer
b.remove(i)
b.toArray
}
回答by Rado Buransky
def dropEle[T](n: Int, in: Array[T]): Array[T] = in.take(n - 1) ++ in.drop(n)
def dropEle[T](n: Int, in: Array[T]): Array[T] = in.take(n - 1) ++ in.drop(n)
回答by Xavier Guihot
Most collections have a patchmethod which can be "abused" to remove an element at a specific index:
大多数集合都有一个patch方法,可以“滥用”删除特定索引处的元素:
Array('a', 'b', 'c', 'd', 'e', 'f', 'g').patch(3, Nil, 1)
// Array('a', 'b', 'c', 'e', 'f', 'g')
This:
这:
drops
1element at index3inserts
Nil(an empty sequence) at index3
删除
1索引处的元素3Nil在索引处插入(空序列)3
which in other words means "patching 1 element at index 3 with an empty sequence".
换句话说,这意味着“用空序列修补索引 3 处的 1 个元素”。
Note that here, nis the 0-based index of the item to remove in the collection.
请注意,这里n是要在集合中删除的项目的从 0 开始的索引。
回答by elm
For nth=0referring to the first element in the array,
为了nth=0引用数组中的第一个元素,
def dropEle[T](nth: Int, in: Array[T]): Array[T] = {
in.view.zipWithIndex.filter{ e => e._2 != nth }.map{ kv => kv._1 }.toArray
}
A slightly more compact syntax includes
稍微紧凑的语法包括
def dropEle[T](nth: Int, in: Array[T]): Array[T] = {
in.view.zipWithIndex.filter{ _._2 != nth }.map{ _._1 }.toArray
}

