scala 创建可变集合_如何在Scala中创建可变集?

本文介绍了Scala中的可变集合,指出集合中的元素是唯一的,并提供了一个示例展示如何在Scala中声明和使用可变集合。
摘要由CSDN通过智能技术生成

scala 创建可变集合

Scala集 (Scala Set)

In Scala, a Set is a collection of elements of the same type. All elements of the set are unique i.e. no elements are allowed. Sets can be mutable as well as immutable.

在Scala中,集合是相同类型元素的集合。 集合中的所有元素都是唯一的,即不允许任何元素。 集可以是可变的,也可以是不变的。

Example:

例:

    Set(1, 4, 5, 7, 12, 87, 213)

The mutable set is a set that contains elements that can be changed after the initialization of the set.

可变集是一个包含元素的集合,这些元素可以在初始化之后更改。

A mutable variable in the Scala is declared using the val keyword. And mutable Sets in scala are imported from,

Scala中的可变变量是使用val关键字声明的。 Scala中的可变集是从

    scala.collection.mutable.Set

Example:

例:

object MyClass {
    def main(args: Array[String]) {
        val set = scala.collection.mutable.Set(2, 56, 577,12 , 46, 19);
        println("The set is : "+set)
        set += 34;
        set += 99;
        println("After adding two elements, the set is "+ set)
    }
}

Output

输出量

The set is : HashSet(577, 2, 19, 56, 12, 46)
After adding two elements, the set is HashSet(577, 2, 34, 19, 99, 56, 12, 46)


翻译自: https://www.includehelp.com/scala/how-to-create-a-mutable-set-in-scala.aspx

scala 创建可变集合

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值