Scala中的ArrayBuffer(创建可变数组)

Scala中的ArrayBuffer是可变的,允许添加和删除元素。要使用ArrayBuffer,需要导入相关库。可以通过+=运算符或append()方法添加元素,使用-=运算符或remove()方法删除元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Scala中的ArrayBuffer (ArrayBuffer In Scala)

In Scala, arrays are immutable and contain homogenous elements i.e. the size of the array cannot be changed and all the elements of the array contain the same elements.

在Scala中, 数组是不可变的,并且包含同质元素,即数组的大小无法更改,并且数组的所有元素都包含相同的元素。

ArrayBuffer is a special class the is used to create a mutable array.

ArrayBuffer是一个特殊的类,用于创建可变数组。

To use the ArrayBuffer we will import,

要使用ArrayBuffer,我们将导入

    scala.collection.mutable.ArrayBuffer

Syntax:

句法:

Syntax to create an ArrayBuffer in Scala,

在Scala中创建ArrayBuffer的语法,

    var arrayBuffer = ArrayBuffer("element1", "element2", ...)

程序创建一个ArrayBuffer (Program to create an ArrayBuffer)

import scala.collection.mutable.ArrayBuffer

object MyClass {
    def main(args: Array[String]) {
        println("My bikes are ")
        val bikes = ArrayBuffer("ThunderBird 350", "YRF R3")
        for(i <- 0 to bikes.length-1)
            println(bikes(i))

        // adding a string 
        bikes += "iron 883"
        println("After adding new bike to my collection")
        for(i <- 0 to bikes.length-1)
            println(bikes(i))
        
    }
}

Output

输出量

My bikes are 
ThunderBird 350
YRF R3
After adding new bike to my collection
ThunderBird 350
YRF R3
iron 883

You can add new element(s) to the ArrayBuffer using the += operator and using the append() method.

您可以使用+ =运算符并使用append()方法将新元素添加到ArrayBuffer中

向ArrayBuffer添加新元素 (Adding new elements to ArrayBuffer)

import scala.collection.mutable.ArrayBuffer

object MyClass {
    def main(args: Array[String]) {
        println("My bikes are ")
        val bikes = ArrayBuffer[String]()
        
        bikes += "ThunderBird 350"
        bikes += ("YRF R3", "Iron 883")
        bikes.append("BMW S1000 RR")
        
        for(i <- 0 to bikes.length-1)
            println(bikes(i))
    }
}

Output

输出量

My bikes are 
ThunderBird 350
YRF R3
Iron 883
BMW S1000 RR

删除数组元素 (Deleting array elements)

You can also delete elements of the ArrayBuffer using -= operator and ArrayBuffer.remove() method.

您还可以使用-=运算符和ArrayBuffer.remove()方法删除ArrayBuffer的元素。

Syntax:

句法:

Deleting one element using -= operator

使用-=运算符删除一个元素

    ArrayBuffer -= (arrayElement)

Deleting multiple-elements using -= operator

使用-=运算符删除多个元素

    ArrayBuffer -= (arrayElement1, arrayElement2, ...)

Deleting element using remove method

使用remove方法删除元素

    ArrayBuffer -= (arrayElementposition)

删除ArrayBuffer元素的程序 (Program to delete elements of ArrayBuffer)

import scala.collection.mutable.ArrayBuffer

object MyClass {
    def main(args: Array[String]) {
        val bikes = ArrayBuffer("ThunderBird 350", "YRF R3", "Iron 883", "BMW S1000 RR", "BMW GSA")
        println("My bikes are ")
        for(i <- 0 to bikes.length-1)
            println(bikes(i))
            
        println("Removing elements...")   
        bikes -= "YRF R3"
        bikes -= ("Iron 883", "BMW GSA")
        bikes.remove(1)
        for(i <- 0 to bikes.length-1)
            println(bikes(i))
    }
}

Output

输出量

My bikes are 
ThunderBird 350
YRF R3
Iron 883
BMW S1000 RR
BMW GSA
Removing elements...
ThunderBird 350

These were some operations on ArrayBuffer which is a special class that creates an Array mutable.

这些是对ArrayBuffer的一些操作,它是一个创建Array可变变量的特殊类。

翻译自: https://www.includehelp.com/scala/arraybuffer-in-scala-creating-mutable-arrays.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值