数组和列表在scala中的区别

[英]Difference between Array and List in scala


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?

在什么情况下,我应该使用数组(缓冲区)和列表(缓冲区)。我所知道的唯一区别是数组是非变异体,列表是协变的。但是性能和其他一些特征呢?

3 个解决方案

#1


119  

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 an Array (which is actually mutable - the immutable analog of Array is IndexedSeq).

Scala列表是一个不可变的递归数据结构,它是Scala中的一个基本结构,您应该(很可能)使用它而不是一个数组(它实际上是可变的——数组的不可变模拟是IndexedSeq)。

If you are coming from a Java background, then the obvious parallel is when to use LinkedList over ArrayList. 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.

如果您来自Java背景,那么明显的相似之处在于何时在ArrayList上使用LinkedList。前者通常用于只遍历的列表(其大小前面不知道),而后者应该用于具有已知大小(或最大大小)或快速随机访问非常重要的列表。

Mutable Structures

ListBuffer provides a constant-time conversion to a List which is reason alone to use ListBuffer if such later conversion is required.

ListBuffer提供对列表的常量时间转换,如果需要以后的转换,那么仅凭这个原因就可以使用ListBuffer。

A scala Array should be implemented on the JVM by a Java array, and hence an Array[Int] may be much more performant (as an int[]) than a List[Int] (which will box its contents, unless you are using the very latest versions of Scala which have the new @specialized feature).

scala数组应该通过Java数组在JVM上实现,因此数组[Int]可能比List[Int]更具有性能(作为Int[])(除非使用最新版本的scala,具有新的@专门化特性)。

However, I think that the use of Arrays 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.

然而,我认为使用数组在Scala中应该保持最小,因为感觉你真正需要知道的是在幕后决定是否你的数组将支持所需的原始类型,或可能是盒装作为包装器类型。

#2


109  

In addition to the answers posted already, here are some specifics.

除了已经发布的答案,这里还有一些细节。

While an Array[A] is literally a Java array, a List[A] is an immutable data structure that is either Nil (the empty list) or consists of a pair (A, List[A]).

虽然数组[A]实际上是一个Java数组,但List[A]是一个不可变的数据结构,它要么是Nil(空列表),要么是一对(A, List[A])。

Performance differences

性能的差异

                          Array  List
Access the ith element    O(1)   O(i)
Delete the ith element    O(n)   O(i)
Insert an element at i    O(n)   O(i)
Reverse                   O(n)   O(n)
Concatenate (length m,n)  O(n+m) O(n)
Count the elements        O(1)   O(n)

Memory differences

记忆的差异

                          Array  List
Get the first i elements  O(i)   O(i)
Drop the first i elements O(n-i) O(1)
Insert an element at i    O(n)   O(i)
Reverse                   O(n)   O(n)
Concatenate (length m,n)  O(n+m) O(n)

So unless you need rapid random access or need to count elements, a List is better than an Array.

因此,除非您需要快速的随机访问或需要计数元素,否则列表比数组更好。

#3


15  

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.

数组是可变的,这意味着您可以更改每个索引的值,而列表(默认情况下)是不可变的,这意味着每次进行修改时都会创建一个新列表。在大多数情况下,使用不可变的数据类型是一种更“功能性”的样式,您应该尝试使用一个具有诸如yield、foreach、match等结构的列表。

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.

对于性能特征,数组在随机访问元素时速度更快,而列表在预挂(添加)新元素时速度更快。迭代它们是可比较的。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2010/04/26/720181e7589119f5a455550276bcdcc5.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告