Scala 在不复制ArraySeq的情况下预结束ArraySeq
在Scala 在不复制ArraySeq的情况下预结束ArraySeq,scala,seq,Scala,Seq,在ArraySeq中的+:方法的文档中说 带有前置元素的序列副本 有没有一种方法可以在不复制整个ArraySeq的情况下预先添加元素?即使ArraySeq是一个可变集合,该集合的某些成员方法也会返回集合的副本,而不会进行“就地”转换。从 已知包scala.collection.mutable中的集合有一些 更改集合的操作。那么处理 可变集合意味着您需要了解哪些代码发生了更改 什么时候收藏 因此,可变集合可能会有一些方法返回原始集合的副本 另一方面,它保证scala.collection.immu
ArraySeq
中的+:
方法的文档中说
带有前置元素的序列副本
有没有一种方法可以在不复制整个
ArraySeq
的情况下预先添加元素?即使ArraySeq是一个可变集合,该集合的某些成员方法也会返回集合的副本,而不会进行“就地”转换。从
已知包scala.collection.mutable中的集合有一些
更改集合的操作。那么处理
可变集合意味着您需要了解哪些代码发生了更改
什么时候收藏
因此,可变集合可能会有一些方法返回原始集合的副本
另一方面,它保证scala.collection.immutable包中集合上的所有操作都将返回原始集合的副本。即使ArraySeq是可变集合,此集合的某些成员方法也将返回集合的副本,而不会执行“就地”转换。从 已知包scala.collection.mutable中的集合有一些 更改集合的操作。那么处理 可变集合意味着您需要了解哪些代码发生了更改 什么时候收藏 因此,可变集合可能会有一些方法返回原始集合的副本
另一方面,它保证scala.collection.immutable包中集合上的所有操作都将返回原始集合的副本。您不能。引用(粗体强调): 数组序列是固定大小的可变序列,将其元素存储在
数组[对象]
中。它们在Scala中按类ArraySeq
实现
前置更改大小,因此无法前置到ArraySeq
它们非常类似于数组
s,当然它们也是可变的,并且具有固定的大小
如果要更改大小,需要一个
*Builder
或*Buffer
,在这种情况下,需要一个,它确实有一个预结束方法。您不能。引用(粗体强调):
数组序列是固定大小的可变序列,将其元素存储在数组[对象]
中。它们在Scala中按类ArraySeq
实现
前置更改大小,因此无法前置到ArraySeq
它们非常类似于数组
s,当然它们也是可变的,并且具有固定的大小
如果要更改大小,需要一个
*Builder
或*Buffer
,在本例中,是一个,它确实有一个预结束的方法。这就是数组seq
的工作方式。
AFAIK有没有其他内置选项可用
表示要调整大小,必须返回新副本:(
医生说:
/** Inserts new elements at the index `n`. Opposed to method
* `update`, this method will not replace an element with a
* one. Instead, it will insert a new element at index `n`.
*
* @param n the index where a new element will be inserted.
* @param seq the traversable object providing all elements to insert.
* @throws Predef.IndexOutOfBoundsException if `n` is out of bounds.
*/
def insertAll(n: Int, seq: Traversable[A]) {
if (n < 0 || n > size0) throw new IndexOutOfBoundsException(n.toString)
val xs = seq.toList
val len = xs.length
ensureSize(size0 + len)
copy(n, n + len, size0 - n)
xs.copyToArray(array.asInstanceOf[scala.Array[Any]], n)
size0 += len
}
抽象定义
+:(elem:A):ArraySeq[A][usecase]数组序列的一个副本,前面有一个元素
类型的集合,由元素后跟
此数组序列。定义类GenSeqLike
在中也有类似的功能。
它还可以复制以创建新的。下面是为了更好地理解
片段1:
/** Prepends a single element to this buffer and returns
* the identity of the buffer. It takes time linear in
* the buffer size.
*
* @param elem the element to append.
* @return the updated buffer.
*/
def +=:(elem: A): this.type = {
ensureSize(size0 + 1)
copy(0, 1, size0)
array(0) = elem.asInstanceOf[AnyRef]
size0 += 1
this
}
代码片段2:
/** Inserts new elements at the index `n`. Opposed to method
* `update`, this method will not replace an element with a
* one. Instead, it will insert a new element at index `n`.
*
* @param n the index where a new element will be inserted.
* @param seq the traversable object providing all elements to insert.
* @throws Predef.IndexOutOfBoundsException if `n` is out of bounds.
*/
def insertAll(n: Int, seq: Traversable[A]) {
if (n < 0 || n > size0) throw new IndexOutOfBoundsException(n.toString)
val xs = seq.toList
val len = xs.length
ensureSize(size0 + len)
copy(n, n + len, size0 - n)
xs.copyToArray(array.asInstanceOf[scala.Array[Any]], n)
size0 += len
}
/**在索引“n”处插入新元素。与方法相反
*`update`,此方法不会将元素替换为
*相反,它将在索引“n”处插入新元素。
*
*@param n将插入新元素的索引。
*@param seq提供所有要插入元素的可遍历对象。
*@如果'n'超出范围,则抛出Predef.IndexOutOfBoundsException。
*/
def insertAll(n:Int,seq:Traversable[A]){
如果(n<0 | | n>size0)抛出新的IndexOutOfBoundsException(n.toString)
val xs=序号toList
val len=xs.length
调整大小(尺寸0+len)
副本(n,n+len,size0-n)
copytarray(array.asInstanceOf[scala.array[Any]],n)
尺寸0+=len
}
这就是ArraySeq
的工作原理。
AFAIK有没有其他内置选项可用
表示要调整大小,必须返回新副本:(
医生说:
/** Inserts new elements at the index `n`. Opposed to method
* `update`, this method will not replace an element with a
* one. Instead, it will insert a new element at index `n`.
*
* @param n the index where a new element will be inserted.
* @param seq the traversable object providing all elements to insert.
* @throws Predef.IndexOutOfBoundsException if `n` is out of bounds.
*/
def insertAll(n: Int, seq: Traversable[A]) {
if (n < 0 || n > size0) throw new IndexOutOfBoundsException(n.toString)
val xs = seq.toList
val len = xs.length
ensureSize(size0 + len)
copy(n, n + len, size0 - n)
xs.copyToArray(array.asInstanceOf[scala.Array[Any]], n)
size0 += len
}
抽象定义
+:(elem:A):ArraySeq[A][usecase]数组序列的一个副本,前面有一个元素
类型的集合,由元素后跟
此数组序列。定义类GenSeqLike
在中也有类似的功能。
它还可以复制以创建新的。下面是为了更好地理解
片段1:
/** Prepends a single element to this buffer and returns
* the identity of the buffer. It takes time linear in
* the buffer size.
*
* @param elem the element to append.
* @return the updated buffer.
*/
def +=:(elem: A): this.type = {
ensureSize(size0 + 1)
copy(0, 1, size0)
array(0) = elem.asInstanceOf[AnyRef]
size0 += 1
this
}
代码片段2:
/** Inserts new elements at the index `n`. Opposed to method
* `update`, this method will not replace an element with a
* one. Instead, it will insert a new element at index `n`.
*
* @param n the index where a new element will be inserted.
* @param seq the traversable object providing all elements to insert.
* @throws Predef.IndexOutOfBoundsException if `n` is out of bounds.
*/
def insertAll(n: Int, seq: Traversable[A]) {
if (n < 0 || n > size0) throw new IndexOutOfBoundsException(n.toString)
val xs = seq.toList
val len = xs.length
ensureSize(size0 + len)
copy(n, n + len, size0 - n)
xs.copyToArray(array.asInstanceOf[scala.Array[Any]], n)
size0 += len
}
/**在索引“n”处插入新元素。与方法相反
*`update`,此方法不会将元素替换为
*相反,它将在索引“n”处插入新元素。
*
*@param n将插入新元素的索引。
*@param seq提供所有要插入元素的可遍历对象。
*@如果'n'超出范围,则抛出Predef.IndexOutOfBoundsException。
*/
def insertAll(n:Int,seq:Traversable[A]){
如果(n<0 | | n>size0)抛出新的IndexOutOfBoundsException(n.toString)
val xs=序号toList
val len=xs.length
调整大小(尺寸0+len)
副本(n,n+len,size0-n)
copytarray(array.asInstanceOf[scala.array[Any]],n)
尺寸0+=len
}
我不这么认为。您可以将ArrayBuffer等用于就地追加/预结束等@Samar,但它是可变的,我们可以使用它