什么时候应该使用Scala的Array而不是其他集合之一? [英] When should I use Scala's Array instead of one of the other collections?

查看:77
本文介绍了什么时候应该使用Scala的Array而不是其他集合之一?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这更多是关于样式和偏好的问题,但是这里有:我什么时候应该使用scala.Array?我一直在使用List,偶尔会遇到Seq,Map之类的东西,但是我从没用过,也从未见过Array.只是为了实现Java兼容性吗?我是否缺少常见用例?

This is more a question of style and preference but here goes: when should I use scala.Array? I use List all the time and occasionally run into Seq, Map and the like, but I've never used nor seen Array in the wild. Is it just there for Java compatibility? Am I missing a common use-case?

推荐答案

首先,让我们在此声明免责声明.Scala 2.7的 Array 尝试同时成为Java Array 和Scala集合.它大多成功,但在某些极端情况下都失败.不幸的是,这些优秀的案例可能发生在具有正常代码的优秀人员身上,因此Scala 2.8与此背离了.

First of all, let's make a disclaimer here. Scala 2.7's Array tries to be a Java Array and a Scala Collection at the same time. It mostly succeeds, but fail at both for some corner cases. Unfortunately, these corner cases can happen to good people with normal code, so Scala 2.8 is departing from that.

在Scala 2.8上,有一个 Array ,它是Java Array .这意味着它是一个连续的存储空间,可以存储引用或原语(因此可能具有不同的元素大小),并且可以非常快速地进行随机访问.它还具有糟糕的方法,可怕的toString实现,并且在同时使用泛型和基元时表现不佳(例如: def f [T](a:Array [T])=...; f(Array(1,2,3))).

On Scala 2.8, there's Array, which is Java Array. That means it is a contiguous memory space, which stores either references or primitives (and, therefore, may have different element sizes), and can be randomly accessed pretty fast. It also has lousy methods, an horrible toString implementation, and performs badly when using generics and primitives at the same time (eg: def f[T](a: Array[T]) = ...; f(Array(1,2,3))).

然后是 GenericArray ,它是一个由 Array 支持的Scala集合.它总是存储盒装基元,因此在混合基元和泛型时不会出现性能问题,但是另一方面,它没有纯基元(非泛型)基元数组的性能提升.

And, then, there is GenericArray, which is a Scala Collection backed by an Array. It always stores boxed primitives, so it doesn't have the performance problems when mixing primitives and generics but, on the other hand, it doesn't have the performance gains of a purely primitive (non-generic) primitive array.

那么,什么时候使用什么? Array 具有以下特征:

So, when to use what? An Array has the following characteristics:

  • O(1)随机读写
  • O(n)追加/添加/插入/删除
  • 可变

如果您不需要泛型,或者您的泛型可以表示为 [T< ;: AnyRef] ,因此排除了 AnyVal 等原语以及那些特性对于您的代码来说是最佳的,然后继续努力.

If you don't need generics, or your generics can be stated as [T <: AnyRef], thus excluding primitives, which are AnyVal, and those characteristics are optimal for your code, then go for it.

如果您确实需要泛型(包括基元),并且这些特征最适合您的代码,请在Scala 2.8上使用 GenericArray .另外,如果您想要一个真正的Collection及其所有方法,则可能也要使用它,而不是依赖于隐式转换.

If you do need generics, including primitives, and those characteristics are optimal for your code, use GenericArray on Scala 2.8. Also, if you want a true Collection, with all of its methods, you may want to use it as well, instead of depending on implicit conversions.

如果您希望具有不变性,或者需要良好的性能进行追加,添加,插入或删除操作,请寻找其他集合.

If you want immutability or if you need good performance for append, prepend, insert or delete, look for some other collection.

这篇关于什么时候应该使用Scala的Array而不是其他集合之一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆