斯卡拉 |数组缓冲区
scala中的数组是同质且可变的,即它包含相同数据类型的元素,其元素可以改变但数组大小的大小不能改变。要创建一个可变的索引序列,其大小可以改变ArrayBuffer类使用。要使用 ArrayBuffer,导入scala.collection.mutable.ArrayBuffer类,创建 ArrayBuffer 的实例。
在内部,ArrayBuffer 是一个元素数组,以及存储的当前数组大小。将元素添加到 ArrayBuffer 时,会检查此大小。如果底层数组未满,则直接将元素添加到数组中。如果底层数组已满,则构造一个更大的数组并将所有元素复制到新数组中。关键是新数组的构造大于当前添加所需的大小。
例子:
var name = new ArrayBuffer[datatype]() // empty buffer is created
var name = new ArrayBuffer("chandan", "gfg", "geeksforgeeks")
在上面的例子中,首先,这里创建了一个空缓冲区 datatype 表示数据的类型,例如 integer、 字符串。然后创建包含三个元素的缓冲区,类型为字符串。
ArrayBuffer 上的操作
- 创建 ArrayBuffer 的实例:
// Scala program to create an ArrayBuffer // ArrayBuffer class is imported import scala.collection.mutable.ArrayBuffer // Creating Object object GFG { // Main Method def main(args: Array[String]) { // Instance of ArrayBuffer is created var name = ArrayBuffer[String]() name += "GeeksForGeeks" name += "gfg" name += "Chandan" println(name) } }
输出:ArrayBuffer(GeeksForGeeks, gfg, Chandan)
- 从 ArrayBuffer 访问元素:
元素的访问与数组相同, ArrayBuffer(i)用于访问数组的第 i 个索引元素。// Scala program to access element of ArrayBuffer // ArrayBuffer class is imported import scala.collection.mutable.ArrayBuffer // Creating Object object GFG { // Main Method def main(args: Array[String]) { // Instance of ArrayBuffer is created var name = ArrayBuffer[String]() name += "GeeksForGeeks" name += "gfg" name += "Chandan" // accessing 1th index element of arraybuffer println(name(1)) } }
输出:gfg
- 在 ArrayBuffer 中添加元素:
- 将单个元素添加到缓冲区
ArrayBuffer+=( element)
- 添加两个或更多元素(方法具有可变参数)
ArrayBuffer+= (element1, element2, ..., elementN )
- 附加一个或多个元素(使用可变参数)
ArrayBuffer.append( elem1, elem2, ... elemN)
例子:
// Scala program to add element in ArrayBuffer // ArrayBuffer class is imported import scala.collection.mutable.ArrayBuffer // Creating Object object GFG { // Main Method def main(args: Array[String]) { // Instance of ArrayBuffer is created var name = ArrayBuffer[String]() // adding one element name += "GeeksForGeeks" // add two or more elements name += ("gfg", "chandan") // adding one or more element using append method name.append("S-series", "Ritesh") // printing arraybuffer println(name) } }
输出:ArrayBuffer(GeeksForGeeks, gfg, chandan, S-series, Ritesh)
- 将单个元素添加到缓冲区
- 删除 ArrayBuffer 元素:
- 删除一个元素
ArrayBuffer-= (element)
- 移除多个元素
ArrayBuffer-= (elem1, elem2, ....., elemN)
例子:
// Scala program to delete element from ArrayBuffer // ArrayBuffer class is imported import scala.collection.mutable.ArrayBuffer // Creating Object object GFG { // Main Method def main(args: Array[String]) { // Instance of ArrayBuffer is created var name = ArrayBuffer( "GeeksForGeeks","gfg", "chandan","S-series", "Ritesh" ) // deletes one element name -= "GeeksForGeeks" // deletes two or more elements name -= ("gfg", "chandan") // printing resultant arraybuffer println(name) } }
输出:ArrayBuffer(S-series, Ritesh)
- 删除一个元素
- 使用ArrayBuffer.remove()删除 ArrayBuffer 元素:
remove方法用于按其在 ArrayBuffer 中的位置删除一个元素,或从起始位置开始的一系列元素。
例子:
// Scala program for remove method, on ArrayBuffer // ArrayBuffer class is imported import scala.collection.mutable.ArrayBuffer // Creating Object object GFG { // Main Method def main(args: Array[String]) { // Instance of ArrayBuffer is created var name = ArrayBuffer( "GeeksForGeeks", "gfg", "chandan", "S-series", "Ritesh" ) // removing 0th index element name.remove(0) // printing resultant arraybuffer println(name) name.remove(1, 3) // printing resultant arraybuffer println(name) } }
输出:ArrayBuffer(gfg, chandan, S-series, Ritesh) ArrayBuffer(gfg)