Scala ListBuffer –创建可变列表

Scala列表 (Scala Lists)

A List is a collection of immutable elements of the same data type. In scala, the list represents a linked-list.

列表是相同数据类型的不可变元素的集合。 在scala中,该列表表示一个链接列表。

You cannot update a list as it is immutable, so for functions like add, update, delete we need to create a mutable list which is done using ListBuffer.

您无法更新列表,因为它是不可变的,因此对于诸如add,update,delete之类的功能,我们需要创建一个可变列表,该列表是使用ListBuffer完成的。

Scala ListBuffer (Scala ListBuffer)

ListBuffer is a special type of list which is immutable i.e. updating ListBuffer is possible.

ListBuffer是不可变的特殊列表类型,即可以更新ListBuffer。

Syntax to create an empty ListBuffer in Scala,

在Scala中创建一个空的ListBuffer的语法,

    var ListBuffer_name = new ListBuffer[datatype]()

Syntax to create a ListBuffer with elements in Scala,

使用Scala中的元素创建ListBuffer的语法,

    var ListBuffer_name = new ListBuffer(element1, element2, element3...)

For creating ListBuffer in Scala, we need to import the ListBuffer class to our program using the following statement,

为了在Scala中创建ListBuffer,我们需要使用以下语句将ListBuffer类导入到程序中:

    import scala.collection.mutable.ListBuffer

ListBuffer操作 (ListBuffer operations)

We can perform update operations like adding and deleting elements in a ListBuffer.

我们可以执行更新操作,例如在ListBuffer中添加和删除元素。

1)访问ListBuffer的元素 (1) Accessing elements of a ListBuffer)

We can access any element of the ListBuffer in the same way that we used to access elements in list using the index value of the element in the ListBuffer.

我们可以使用使用ListBuffer中元素的索引值来访问ListBuffer中的元素的方式,就像访问列表中元素的方式一样。

The syntax for accessing ith element of the ListBuffer,

访问ListBuffer的 i 元素的语法,

    listBuffer(i)

Program to access elements of a ListBuffer

程序访问ListBuffer的元素

import scala.collection.mutable.ListBuffer

object MyObject {
    def main(args: Array[String]) {
        var city = ListBuffer("Delhi" , "Mumbai" , "Indore" , "Pune")
        println("Acessing 2nd element of the ListBuffer : " + city(1))
        println("Acessing 3nd element of the ListBuffer : " + city(2))
    }
}

Output:

输出:

Acessing 2nd element of the ListBuffer : Mumbai
Acessing 3nd element of the ListBuffer : Indore

2)将元素添加到ListBuffer (2) Adding elements to a ListBuffer)

Adding of new elements in the same ListBuffer is possible. There are several methods that can be used to add elements to ListBuffer. They are,

可以在同一ListBuffer中添加新元素。 有几种方法可用于将元素添加到ListBuffer。 他们是,

  1. Using += operator

    使用+ =运算符

  2. Using append() method

    使用append()方法

Syntax:

句法:

    //Syntax to add a single element to ListBuffer,
    ListBuffer += element
    //Syntax to add multiple elements to ListBuffer,
    ListBuffer += (element1, element2, element3, ...)

Note: Both the above syntaxes are used to append() the elements to the ListBuffer, i.e. elements will be added at the end of the ListBuffer. But you can also prepend list in Scala i.e. add elements at the start of the ListBuffer using the following syntax,

注意:以上两种语法都用于元素附加到ListBuffer(即,元素),即元素将添加到ListBuffer的末尾。 但是您也可以在Scala中添加列表,即使用以下语法在ListBuffer的开头添加元素,

    element +=: ListBuffer

Program for adding elements to ListBuffer using += operator

使用+ =运算符将元素添加到ListBuffer的程序

import scala.collection.mutable.ListBuffer

object MyObject {
    def main(args: Array[String]) {
        var bikes = ListBuffer[String]()
        println("Empty ListBuffer : " + bikes)
        
        println("Adding a single element to the ListBuffer")
        bikes += "ThunderBird 350"
        println("ListBuffer : " + bikes)
        
        println("Adding multiple element to the listBuffer")
        bikes += ( "Iron 833", "S1000RR" )
        println("ListBuffer : " + bikes)
        
        println("Adding elements at the start of the ListBuffer")
        ("Pulsar 150") +=: bikes
        println("ListBuffer : " + bikes)
    }
}

Output:

输出:

Empty ListBuffer : ListBuffer()
Adding a single element to the ListBuffer
ListBuffer : ListBuffer(ThunderBird 350)
Adding multiple element to the listBuffer
ListBuffer : ListBuffer(ThunderBird 350, Iron 833, S1000RR)
Adding elements at the start of the ListBuffer
ListBuffer : ListBuffer(Pulsar 150, ThunderBird 350, Iron 833, S1000RR)

The append() method is used to add elements at the end of the ListBuffer.

append()方法用于在ListBuffer的末尾添加元素。

Syntax:

句法:

    ListBuffer.append(element1, element2, element3...)

Program for adding elements to ListBuffer using the append() method

使用append()方法将元素添加到ListBuffer的程序

import scala.collection.mutable.ListBuffer

object MyObject {
    def main(args: Array[String]) {
        var lang = ListBuffer[String]()
        println("Empty ListBuffer : " + lang)

        println("Adding a single element to the ListBuffer")
        lang.append("C")
        println("ListBuffer : " + lang)

        println("Adding multiple element to the listBuffer")
        lang.append( "C++", "Scala" , "Python" )
        println("ListBuffer : " + lang)
    }
}

Output:

输出:

Empty ListBuffer : ListBuffer()
Adding a single element to the ListBuffer
ListBuffer : ListBuffer(C)
Adding multiple element to the listBuffer
ListBuffer : ListBuffer(C, C++, Scala, Python)

3)从ListBuffer删除元素 (3) Deleting elements from a ListBuffer)

In ListBuffer, you can delete elements. The are several methods to delete elements from a ListBuffer. They are

在ListBuffer中,您可以删除元素。 有几种从ListBuffer删除元素的方法。 他们是

  1. Using -= operator

    使用-=运算符

  2. Using remove() method

    使用remove()方法

We can delete single or multiple elements using the -= operator.

我们可以使用-=运算符删除单个或多个元素。

Syntax:

句法:

    //Deleting a single element from ListBuffer,
    ListBuffer -= element
    //Deleting multiple elements from ListBuffer,
    ListBuffer -= (element1, element2, element3, ...)

Program to delete elements from a ListBuffer using -= method

程序使用-=方法从ListBuffer删除元素

import scala.collection.mutable.ListBuffer

object MyObject {
    def main(args: Array[String]) {
        var listbuffer = ListBuffer(4, 5, 1, 7 ,21, 43, 99)
        println("ListBuffer : " + listbuffer)

        println("Removing single element to the ListBuffer")
        listbuffer -= 43
        println("ListBuffer : " + listbuffer)

        println("Removing multiple element to the ListBuffer")
        listbuffer -= (4, 99, 1)
        println("ListBuffer : " + listbuffer)
    }
}

Output:

输出:

ListBuffer : ListBuffer(4, 5, 1, 7, 21, 43, 99)
Removing single element to the ListBuffer
ListBuffer : ListBuffer(4, 5, 1, 7, 21, 99)
Removing multiple element to the ListBuffer
ListBuffer : ListBuffer(5, 7, 21)

The remove() method can delete single and multiple elements from the ListBuffer. The element is deleted using its index.

remove()方法可以从ListBuffer中删除单个和多个元素。 使用其索引删除该元素。

Syntax:

句法:

    //Syntax for deleting a single element from ListBuffer, 
    ListBuffer.remove(index)
    //Syntax for deleting multiple elements from ListBuffer, 
    ListBuffer.remove(startindex , endindex)

Program to delete elements from ListBuffer using remove() method

程序使用remove()方法从ListBuffer删除元素

import scala.collection.mutable.ListBuffer

object MyObject {
    def main(args: Array[String]) {
        var listbuffer = ListBuffer(4, 5, 1, 7 ,21, 43, 99)
        println("ListBuffer : " + listbuffer)
        
        println("Removing single element to the ListBuffer")
        listbuffer.remove(4)
        println("ListBuffer : " + listbuffer)
        
        println("Removing multiple element to the ListBuffer")
        listbuffer.remove(1, 4)
        println("ListBuffer : " + listbuffer)
    }
}

Output:

输出:

ListBuffer : ListBuffer(4, 5, 1, 7, 21, 43, 99)
Removing single element to the ListBuffer
ListBuffer : ListBuffer(4, 5, 1, 7, 43, 99)
Removing multiple element to the ListBuffer
ListBuffer : ListBuffer(4, 99)


翻译自: https://www.includehelp.com/scala/listbuffer-creating-mutable-list.aspx

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值