2014年03月04日 星期二 09:44
ArrayBuffer是一种mutable的数据容器,相对于Array来说,最大的区别就是可以自由增删元素。当ArrayBuffer构建完毕后,还可以转换为immutable的Array容器。
An ArrayBuffer is like an array, except that you can additionally add and remove elements from the beginning and end of the sequence. All Array operations are available, though they are a little slower due to a layer of wrapping in the implementation. The new addition and removal operations are constant time on average, but occasionally require linear time due to the implementation needing to allocate a new array to hold the buffer's contents.
示例代码如下:
import scala.collection.mutable.ArrayBuffer
object S014_ArrayBuffer {
def main(args: Array[String]): Unit = {
val ab=ArrayBuffer[Int]()
ab += 100
ab += 200
ab.append(300)
ab.prepend(10)
ab.insert(2, 150)
ab.foreach(println)
for(x <- 1000 to 10000 by 100){
ab.append(x)
}
val ary=ab.toArray
for(i <- ary){
println("ary: " + i.toString)
}
}
}
参考资料:
Zeuux © 2025
京ICP备05028076号