将元素添加到向量的末尾 [英] Adding Element to End of Vector

查看:66
本文介绍了将元素添加到向量的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scaladocs 解释了如何将元素添加到向量.

Scaladocs explain how to add an element to a Vector.

def :+(elem: A): Vector[A]
[use case] A copy of this vector with an element appended.

示例:

scala> Vector(1,2) :+ 3
res12: scala.collection.immutable.Vector[Int] = Vector(1, 2, 3)

对于大型集合,复制整个 Vector,然后向其中添加元素似乎很昂贵.

For a large collection, it seems expensive to copy the whole Vector, and then add an element to it.

向 Vector 添加元素的最佳(最快)方法是什么?

What's the best(fastest) way to add an element to a Vector?

推荐答案

连接到一个不可变的 Vector 是 O(logN).看看这篇论文,看看它是如何完成的.

Concatenation to an immutable Vector is O(logN). Take a look at this paper to see how it is done.

http://infoscience.epfl.ch/record/169879/files/RMTrees.pdf

这篇关于将元素添加到向量的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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