4、Scala数据结构List

本文介绍Scala中的List数据结构。

1、基本知识

Scala中List是存储元素类型都相同的数据结构。

List和Array很相似,但有两点不同。首先,List是不可变的,Array是可变的。其次,List是链式存储(linked list)的列表,而Array是flat存储的。 

List的index是从 0 开始的,最大为长度-1。

2、List的定义/声明方法

2.1 直接通过赋值实例化

//in scala
val studentNames:List[String] = List("Rohan", "Andreas", "Rob", "John")

创建一个空的List

//in scala
val em: List[Nothing] = List()

2.2 创建一个多维的List 

//in scala
val twodim: List[List[Int]] =
      List(
        List(1, 0, 0),
        List(0, 1, 0),
        List(0, 0, 1)
      )
println(twodim)

2.3 填充的方法创建元素全部相同的List

List.fill(N)(Value)方法,N参数表示列表List的长度,Value参数表示填充的数据。

//in scala
val name = List.fill(6)("Rehan")
println( "Name : " + name  )

val id = List.fill(6)(12)
println( "Id : " + id  )

3、List上的基本操作

3.1  List.head 返回List中第一个元素

3.2 List.tail 返回除了第一个元素外的剩下所有元素

3.3 List.isEmpty 判断List是不是空的,返回true or false

// in scala
object Test {
  def main(args:Array[String]) {
    val names= "Harry" :: ("Adam" :: ("Jill" :: Nil))
    val age = Nil
    println( "Head of names array : " + names.head )
    println( "Tail of names array : " + names.tail )
    println( "Check if names is empty : " + names.isEmpty )
    println( "Check if age is empty : " + age.isEmpty )
  }
}

4、拼接多个List

拼接多个List的方法有操作符 ::: 或者操作符 .:::()  或者方法 concat() 

其中操作符::: 的实现效果和 concat() 是一样的都是把后面的List链接到前面List的后面。

而操作符.:::() 的实现效果确实 将后面的List 链接到前面list的前面。

实际应用中,如果需要拼接的两个List有排列顺序的不同,需要保证顺序的关系,那么就需要关注下到底该用哪种方式拼接。

如果要拼接的List并无实际业务上的顺序关系,那是可以不用区分到底用哪种方法来拼接的。

//in scala
object Test {
  def main(args:Array[String]) {
    val country_1 =  List("India","SriLanka","Algeria")
    val country_2 = List("Austria","Belgium","Canada")

    val country = country_1 ::: country_2
    println( "country_1 ::: country_2 : " + country )

    val cont = country_1.:::(country_2)
    println( "country_1.:::(country_2) : " + cont )
    val con = List.concat(country_1, country_2)
    println( "List.concat(country_1, country_2) : " + con  )

  }
}

5、List逆序排列

List.reverse方法将list中所有元素反序。

//in scala
val country = List("Denmark","Sweden","France")
println( country.reverse)

6、List上的其他常用操作

// in scala
object Test {
  def main(args:Array[String]) {
    val country = List("Denmark","Sweden","France","Toby","Gao","Toby")
    println( country.distinct) //去重
    println( country.indexOf("Toby")) //查找元素的索引位置,默认查找第一个
    println( country.indexOf("Toby",4)) //查找元素的索引位置,从索引位置>=4开始查找
    println( country.length) //List长度
    println( country.sorted) //升序排列
    println( country.sorted.reverse) //降序排列
    println( country.toString()) //List转为一个String
    println( country.min)
    println( country.max)
    println( country.head) //查看第一个元素
    println( country.last) //查看最后一个元素
    println( country.tail) //查看除了第一个元素外的所有元素
    println( country.lastIndexOf("Toby")) //查找一个元素在List中出现的最后一个索引
    println( country.lastIndexOf("Toby",4)) //查找一个元素在List中索引位置4之前出现的最后一个索引
    println( country.toMap[Int,String](???)) //List 转为Map

    val scores = List(12,23,53,23,14)
    println( scores.sum) //List中元素累加
    println( scores.min)
    println( scores.max)

  }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值