问题描述
我对Scala还是陌生的,尝试理解易变的 Seq
。由于它位于软件包 mutable
中,我希望有一种方法可以让我们在不复制整个集合的情况下追加元素。
I'm pretty new to Scala and try to understand mutable Seq
. Since it's in package mutable
I expected there is a method that allows us to append element without copying the whole collection.
但是 mutable.Seq $ c中没有
+ =
方法$ c>,但在 Buffer
中。 :+
和 +:
都复制了集合。
But there is no +=
method in the mutable.Seq
, but in Buffer
is. :+
and +:
both copy the collection.
为什么它是可变的?
推荐答案
因为可变
和 growable
不是一回事。
(后者是前者的一种特定类型:所有可增长的东西都是可变的,但并不是所有可变的东西都是可变的。)
Because mutable
and growable
isn't the same thing. (the latter is one specific type of the former: everything, that's growable is mutable, but not everything that's mutable is growable).
mutable.Seq
具有 update
,可让您更改给定索引处的元素,但不会增大或缩小。
Buffer
是 Seq
的特化,既可变又可扩展。
mutable.Seq
has update
, that allows you to change the element at a given index, but it does not grow or shrink. Buffer
is s specialization of Seq
, that is both mutable and growable.
这篇关于了解可变序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!