scala 中数组和列表的区别

发布于 2024-08-30 06:12:19 字数 86 浏览 7 评论 0原文

在什么情况下我应该使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

涙—继续流 2024-09-06 06:12:19

不可变结构

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

Mutable Structures

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

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

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.

执笏见 2024-09-06 06:12:19

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

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

性能差异

                          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)

内存差异

                          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)

因此,除非您需要快速随机访问、需要计算元素数量,或者由于某种原因需要破坏性更新,否则 List 是比数组更好。

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

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 List is better than an Array.

ゃ懵逼小萝莉 2024-09-06 06:12:19

数组是可变的,这意味着您可以更改每个索引的值,而列表(默认情况下)是不可变的,这意味着每次修改时都会创建一个新列表。在大多数情况下,使用不可变数据类型是一种更“实用”的风格,您可能应该尝试使用带有 yieldforeachmatch 等结构的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文