Array vs ArraySeq比较 [英] Array vs ArraySeq comparison

查看:127
本文介绍了Array vs ArraySeq比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个普遍的问题,但是我想知道是否有人可以建议我使用ArrayArraySeq的优势.从我看到的Array是scala对Java Array的表示,并且其API中的成员并不太多,而ArraySeq似乎包含了更丰富的API.

This is a bit of a general question but I was wondering if anybody could advise me on what would be advantages of working with Array vs ArraySeq. From what I have seen Array is scala's representation of java Array and there are not too many members in its API whereas ArraySeq seems to contain a much richer API.

推荐答案

实际上,您可以从四个不同的类中进行选择,以获得类似于数组的可变功能.

There are actually four different classes you could choose from to get mutable array-like functionality.

Array + ArrayOps
WrappedArray
ArraySeq
ArrayBuffer

Array是一个普通的旧Java数组.到目前为止,这是对基元数组进行低级访问的最佳方法.没有开销.由于隐式转换为ArrayOps,它可以像Scala集合那样工作,它可以捕获基础数组,应用适当的方法,并在适当的情况下返回新的数组.但是,由于ArrayOps并非专门用于基元,因此它很慢(与装箱/拆箱总是一样慢).

Array is a plain old Java array. It is by far the best way to go for low-level access to arrays of primitives. There's no overhead. Also it can act like the Scala collections thanks to implicit conversion to ArrayOps, which grabs the underlying array, applies the appropriate method, and, if appropriate, returns a new array. But since ArrayOps is not specialized for primitives, it's slow (as slow as boxing/unboxing always is).

WrappedArray是一个普通的旧Java数组,但是包装在Scala的所有收藏夹中.它与ArrayOps的区别在于WrappedArray返回另一个WrappedArray,因此至少您不必为每次操作都一遍又一遍地重新执行ArrayOps Java基本数组的开销.当您与Java进行大量互操作并且需要传递普通的旧Java数组时,最好使用它,但是在Scala方面,您需要方便地对其进行操作.

WrappedArray is a plain old Java array, but wrapped in all of Scala's collection goodies. The difference between it and ArrayOps is that WrappedArray returns another WrappedArray--so at least you don't have the overhead of having to re-ArrayOps your Java primitive array over and over again for each operation. It's good to use when you are doing a lot of interop with Java and you need to pass in plain old Java arrays, but on the Scala side you need to manipulate them conveniently.

ArraySeq将其数据存储在一个普通的旧Java数组中,但不再存储原语数组.一切都是对象的数组.这意味着原始方法会被装箱.如果要多次使用原始方法,这实际上很方便.由于已经存储了装箱的副本,因此只需要取消装箱,而无需在每个常规操作中对它们进行装箱和取消装箱.

ArraySeq stores its data in a plain old Java array, but it no longer stores arrays of primitives; everything is an array of objects. This means that primitives get boxed on the way in. That's actually convenient if you want to use the primitives many times; since you've got boxed copies stored, you only have to unbox them, not box and unbox them on every generic operation.

ArrayBuffer的行为就像一个数组,但是您可以在其中添加和删除元素.如果您要一直使用ArraySeq,为什么不增加在更改长度时的灵活性?

ArrayBuffer acts like an array, but you can add and remove elements from it. If you're going to go all the way to ArraySeq, why not have the added flexibility of changing length while you're at it?

这篇关于Array vs ArraySeq比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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