Scala:将元素附加到数组的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7500081/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:28:49  来源:igfitidea点击:

Scala: what is the best way to append an element to an Array?

arraysscalaappend

提问by Gregor Scheidt

Say I have an Array[Int]like

说我Array[Int]喜欢

val array = Array( 1, 2, 3 )

Now I would like to append an element to the array, say the value 4, as in the following example:

现在我想将一个元素附加到数组中,比如 value 4,如下例所示:

val array2 = array + 4     // will not compile

I can of course use System.arraycopy()and do this on my own, but there must be a Scala library function for this, which I simply could not find. Thanks for any pointers!

我当然可以System.arraycopy()自己使用和执行此操作,但是必须有一个 Scala 库函数,我根本找不到。感谢您的任何指点!

Notes:

笔记:

  1. I am aware that I can append another Array of elements, like in the following line, but that seems too round-about:

    val array2b = array ++ Array( 4 )     // this works
    
  2. I am aware of the advantages and drawbacks of List vs Array and here I am for various reasons specifically interested in extending an Array.

  1. 我知道我可以附加另一个元素数组,如下行所示,但这似乎太迂回了:

    val array2b = array ++ Array( 4 )     // this works
    
  2. 我知道 List 与 Array 的优缺点,出于各种原因,我对扩展数组特别感兴趣。

Edit 1

编辑 1

Thanks for the answers pointing to the :+operator method. This is what I was looking for. Unfortunately, it is rather slower than a custom append() method implementation using arraycopy-- about two to three times slower. Looking at the implementation in SeqLike[], a builder is created, then the array is added to it, then the append is done via the builder, then the builder is rendered. Not a good implementation for arrays. I did a quick benchmark comparing the two methods, looking at the fastest time out of ten cycles. Doing 10 million repetitions of a single-item append to an 8-element array instance of some class Footakes 3.1 sec with :+and 1.7 sec with a simple append()method that uses System.arraycopy();doing 10 million single-item append repetitions on 8-element arrays of Long takes 2.1 sec with :+and 0.78 sec with the simple append()method. Wonder if this couldn't be fixed in the library with a custom implementation for Array?

感谢您提供指向:+操作符方法的答案。这就是我一直在寻找的。不幸的是,它比使用自定义 append() 方法实现arraycopy要慢——大约慢两到三倍。查看 中的实现SeqLike[],创建了一个构建器,然后将数组添加到其中,然后通过构建器完成追加,然后呈现构建器。不是一个好的数组实现。我做了一个比较两种方法的快速基准测试,查看十个周期中最快的时间。将单个项目附加到某个类的 8 元素数组实例的 1000 万次重复Foo需要 3.1 秒,:+使用一个简单的append()方法需要1.7 秒System.arraycopy();在 Long 的 8 元素数组上执行 1000 万次单项追加重复需要 2.1 秒,:+使用简单append()方法需要0.78 秒。想知道这是否无法通过自定义实现在库中修复Array

Edit 2

编辑 2

For what it's worth, I filed a ticket: https://issues.scala-lang.org/browse/SI-5017

对于它的价值,我提交了一张票:https: //issues.scala-lang.org/browse/SI-5017

回答by tenshi

You can use :+to append element to array and +:to prepend it:

您可以使用:+将元素附加到数组并添加+:它:

0 +: array :+ 4

should produce:

应该产生:

res3: Array[Int] = Array(0, 1, 2, 3, 4)

It's the same as with any other implementation of Seq.

它与Seq.

回答by Landei

val array2 = array :+ 4
//Array(1, 2, 3, 4)

Works also "reversed":

作品也“颠倒”:

val array2 = 4 +: array
Array(4, 1, 2, 3)

There is also an "in-place" version:

还有一个“就地”版本:

var array = Array( 1, 2, 3 )
array +:= 4
//Array(4, 1, 2, 3)
array :+= 0
//Array(4, 1, 2, 3, 0)

回答by Nicolas

The easiest might be:

最简单的可能是:

Array(1, 2, 3) :+ 4

Actually, Array can be implcitly transformed in a WrappedArray

实际上,Array 可以隐式转换为 WrappedArray