Scala学习——列表List的连接

Scala中列表(List)的连接有三种方法:

1、使用操作符“:::”(注:是3个冒号)

2、使用方法List.:::()

3、使用方法List.concat()

举个栗子:

object Demo {
    def main(args: Array[String]) {

        //创建List
        val fruit1 = "apples" :: ("oranges" :: ("pears" :: Nil))
        val fruit2 = "mangoes" :: ("banana" :: Nil)

        // 使用 ::: 操作符连接两个或者多个列表    
        var fruit = fruit1 ::: fruit2
        println( "fruit1 ::: fruit2 : " + fruit )
      
        // 使用集合.:::()方法连接两个列表
        fruit = fruit1.:::(fruit2)
        println( "fruit1.:::(fruit2) : " + fruit )

        // 通过两个或多个列表作为参数。
        fruit = List.concat(fruit1, fruit2)
        println( "List.concat(fruit1, fruit2) : " + fruit)
    }
}

运行结果应如下:

fruit1:::fruit2:List(apples,oranges,pears,mangoes,banana)

fruit1.:::(fruit2):List(mangoes,banana,apples,oranges,pears)

List.concat(fuirt1,fruit2):List(apples,oranges,pears,mangoes,banana)

注:

A:::B

在结果返回的列表中,列表A的元素在前,列表B的元素在后

A.:::(B)

在结果返回的列表中,列表B的元素在前,列表A的元素在后

List.concat(A,B)

在结果返回的列表中,列表A的元素在前,列表B的元素在后

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值