數組和列表在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.

對於性能特征,數組在隨機訪問元素時速度更快,而列表在預掛(添加)新元素時速度更快。迭代它們是可比較的。


注意!

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



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