list scala中数组和列表的区别

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

Difference between Array and List in scala

arrayslistscalascala-collections

提问by Jeriho

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?

在什么情况下我应该使用 Array(Buffer) 和 List(Buffer)。我知道的唯一一个区别是数组是非变量的,而列表是协变量的。但是性能和其他一些特性呢?

回答by oxbow_lakes

Immutable Structures

不可变结构

The Scala Listis 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 analogof Arrayis IndexedSeq).

斯卡拉List是不可变的递归数据结构,它是Scala这样的基本结构,你应该(可能)使用它远远超过一个是Array(这实际上是可变的-的不可变模拟ArrayIndexedSeq)。

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

如果您来自 Java 背景,那么明显的相似之处是何时使用LinkedListover ArrayList。前者通常用于只遍历过(并且其大小预先未知)的列表,而后者应该用于具有已知大小(或最大大小)或快速随机访问很重要的列表。

Mutable Structures

可变结构

ListBufferprovides a constant-time conversion to a Listwhich is reason alone to use ListBufferif such later conversion is required.

ListBuffer提供到 a 的恒定时间转换,如果需要稍后的转换,则可以List单独使用ListBuffer

A scala Arrayshould 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 @specializedfeature).

ScalaArray应该通过 Java 数组在 JVM 上实现,因此 anArray[Int]可能int[]比 a性能更高(作为)List[Int](它将装箱其内容,除非您使用具有新@specialized功能的最新版本的 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.

但是,我认为Array在 Scala 中使用s 应该保持最少,因为感觉你真的需要知道幕后发生了什么来决定你的数组是否真的会得到所需的原始类型的支持,或者可能作为包装类型装箱。

回答by Apocalisp

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]).

虽然 anArray[A]字面上是 Java 数组,但 aList[A]是不可变的数据结构,它是Nil(空列表)或由 pair 组成(A, List[A])

Performance differences

性能差异

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

Memory differences

内存差异

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

So unless you need rapid random access, need to count elements, or for some reason you need destructive updates, a Listis better than an Array.

因此,除非您需要快速随机访问,需要对元素进行计数,或者出于某种原因需要破坏性更新,否则 aListArray.

回答by leonm

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, matchand so forth.

Array 是可变的,这意味着您可以更改每个索引的值,而 List(默认情况下)是不可变的,这意味着每次进行修改时都会创建一个新列表。在大多数情况下,它是一个更“实用”的风格与不可改变的数据类型的工作,你或许应该尝试使用列表中包含结构yieldforeachmatch等等。

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.

对于性能特征,随机访问元素时 Array 更快,而在添加(添加)新元素时 List 更快。迭代它们是可比的。