我想在我的程序中使用ArrayBuffer来保存数字列表.我想用它作为队列.每次删除列表中的第一项并使用它.然后我想知道每次删除第一个项目时,队列中的所有其他数字都会移动一个地方.我的意思是下次我可以再次读取项目(0)吗?
解决方法:
如果您查看Scala source code或ArrayBuffer,您将看到以下remove的实现:
/** Removes the element on a given index position. It takes time linear in
* the buffer size.
*
* @param n the index which refers to the first element to delete.
* @param count the number of elements to delete
* @throws Predef.indexoutofboundsexception if `n` is out of bounds.
*/
override def remove(n: Int, count: Int) {
require(count >= 0, "removing negative number of elements")
if (n < 0 || n > size0 - count) throw new indexoutofboundsexception(n.toString)
copy(n + count, n, size0 - (n + count))
reducetoSize(size0 - count)
}
因此,删除的元素将不再存在于数组中,但实际上,删除意味着复制所有元素(在定义中“移位”),对于较长的数组,它将会很慢.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。