scala 中数组和列表的区别
在什么情况下我应该使用Array(Buffer)和List(Buffer)。我知道的唯一一个区别是数组是不变的而列表是协变的。但是性能和其他一些特性又如何呢?
In what cases I should use Array(Buffer) and List(Buffer). Only one difference that I know is that arrays are nonvariant and lists are covariant. But what about performance and some other characteristics?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不可变结构
Scala
List
是一个不可变的递归数据结构,它是 Scala 中的基本结构,您应该(可能)更多地使用它而不是Array
(实际上是可变 -Array
的不可变模拟是IndexedSeq
)。如果您有 Java 背景,那么明显的相似之处是何时使用
LinkedList
而不是ArrayList
。前者通常用于仅遍历的列表(并且其大小预先未知),而后者应用于具有已知大小(或最大大小)或具有已知大小(或最大大小)的列表快速随机访问很重要。可变结构
ListBuffer
提供了到List
的恒定时间转换,这就是在需要此类后续转换时使用ListBuffer
的唯一原因。scala
Array
应该通过 Java 数组在 JVM 上实现,因此Array[Int]
的性能可能更高(如int[]< /code>)而不是
List[Int]
(它将对其内容进行装箱,除非您使用的是具有新的@specialized
功能的最新版本的 Scala)。然而,我认为 Scala 中数组的使用应该保持在最低限度,因为感觉你真的需要知道幕后发生了什么来决定你的数组是否真的会得到支持按所需的原始类型,或者可以装箱为包装类型。
Immutable Structures
The Scala
List
is an immutable recursive data structure which is such a fundamental structure in Scala, that you should (probably) be using it much more than anArray
(which is actually mutable - the immutable analog ofArray
isIndexedSeq
).If you are coming from a Java background, then the obvious parallel is when to use
LinkedList
overArrayList
. The former is generally used for lists which are only ever traversed (and whose size is not known upfront) whereas the latter should be used for lists which either have a known size (or maximum size) or for which fast random access is important.Mutable Structures
ListBuffer
provides a constant-time conversion to aList
which is reason alone to useListBuffer
if such later conversion is required.A scala
Array
should be implemented on the JVM by a Java array, and hence anArray[Int]
may be much more performant (as anint[]
) than aList[Int]
(which will box its contents, unless you are using the very latest versions of Scala which have the new@specialized
feature).However, I think that the use of
Array
s in Scala should be kept to a minimum because it feels like you really need to know what is going on under the hood to decide whether your array really will be backed by the required primitive type, or may be boxed as a wrapper type.除了已经发布的答案之外,这里还有一些细节。
虽然
Array[A]
实际上是一个Java数组,但List[A]
是一个不可变的数据结构,它要么是Nil
(空的) list) 或由一对(A, List[A])
组成。性能差异
内存差异
因此,除非您需要快速随机访问、需要计算元素数量,或者由于某种原因需要破坏性更新,否则
List
是比数组
更好。In addition to the answers posted already, here are some specifics.
While an
Array[A]
is literally a Java array, aList[A]
is an immutable data structure that is eitherNil
(the empty list) or consists of a pair(A, List[A])
.Performance differences
Memory differences
So unless you need rapid random access, need to count elements, or for some reason you need destructive updates, a
List
is better than anArray
.数组是可变的,这意味着您可以更改每个索引的值,而列表(默认情况下)是不可变的,这意味着每次修改时都会创建一个新列表。在大多数情况下,使用不可变数据类型是一种更“实用”的风格,您可能应该尝试使用带有
yield
、foreach
、match 等结构的 List
等等。就性能特征而言,数组在随机访问元素时速度更快,而列表在添加(添加)新元素时速度更快。迭代它们是可比的。
An Array is mutable, meaning you can change the values of each index, while a List (by default) is immutable, meaning that a new list is created every time you do a modification. In most cases it is a more "functional" style to work with immutable datatypes and you should probably try and use a List with constructs like
yield
,foreach
,match
and so forth.For performance characteristics, an Array is faster with random access to elements, whereas a List is faster when prepending (adding) new elements. Iterating over them is comparable.