Scala学习 - List操作

  List和数组是非常相似的。列表的所有元素都具有相同的类型。二者的区别在于:1、List是不可变的,这意味着List的元素无法通过分配进行更改。List代表一个链表,而数组是平面的。

     创建一个List:

// 字符串列表
val stringList: List[String] = List("Monday", "Tuesday", "Wednesday")

// 整型列表
val numList: List[Int] = List(1, 2, 3, 4)

// 二维列表
val dim: List[List[Int]] =
   List(
      List(1, 0, 0),
      List(0, 1, 0),
      List(0, 0, 1)
   )

  两种方式创建一个空列表:

1、 scala> val emptyList = Nil
emptyList: scala.collection.immutable.Nil.type = List()

2、 scala> val emptyList2 = List()
emptyList2: List[Nothing] = List()

  用字符串创建列表:

scala> var a = "a"::"b"::"c"::Nil;
a: List[String] = List(a, b, c)

  使用 ::: 创建一个新列表

scala> numsList
res13: List[Int] = List(1, 2, 3, 4)

scala> stringList
res14: List[String] = List(a, b, c)

scala> var newList = numsList ::: stringList
newList: List[Any] = List(1, 2, 3, 4, a, b, c)

  过滤获得 numList 元素中大与2的元素

scala> var numsList :List[Int] = List(1, 2, 3, 4)
numsList: List[Int] = List(1, 2, 3, 4)

scala> numsList.filter(n => n>2)
res26: List[Int] = List(3, 4)

  去掉a中头两个元素的新列表

scala> a
res27: List[String] = List(a, b, c)

scala> a.drop(2)
res28: List[String] = List(c)

scala> a
res29: List[String] = List(a, b, c)

  去掉a中后两个元素的新列表

scala> a
res27: List[String] = List(a, b, c)

scala> a.dropRight(2)
res31: List[String] = List(a)

  判断列表中是否有以 A 开头的元素

scala> list
res32: List[String] = List(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

scala> list.forall(s => s.startsWith("A"))
res40: Boolean = false

scala> val sameHead = "ASD" :: "AXC" :: "AER" :: Nil;
sameHead: List[String] = List(ASD, AXC, AER)

scala> sameHead.forall(s => s.startsWith("A"))
res41: Boolean = true

  判断所有元素是否以 y 结尾

scala> list
res32: List[String] = List(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

scala> list.forall(s => s.endsWith("y"))
res40: Boolean = true

scala> val sameHead = "ASD" :: "AXC" :: "AER" :: Nil;
sameHead: List[String] = List(ASD, AXC, AER)

scala> sameHead.forall(s => s.endsWith("y"))
res41: Boolean = false

  打印每个元素

scala> list.foreach(s => print(s + ' '))
Monday Tuesday Wednesday Thursday Friday Saturday Sunday 

  获取 list 中第一个元素

scala> list
res49: List[String] = List(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

list.head
res46: String = Monday

  获取 list中最后一个元素

scala> list
res49: List[String] = List(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

list.last
res47: String = Sunday

  获取 list 中剔除第一个元素,其他的元素的列表

scala> list
res49: List[String] = List(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

scala> list.tail
res48: List[String] = List(Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

  编辑每个元素并将每个元素反转,生成新的列表

scala> list
res49: List[String] = List(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

scala> list.map(a => {val s1 = "abc :: " + a; s1.reverse})
res53: List[String] = List(yadnoM :: cba, yadseuT :: cba, yadsendeW :: cba, yadsruhT :: cba, yadirF :: cba, yadrutaS :: cba, yadnuS :: cba)

  生成逗号分隔的字符串

scala> list
res59: List[String] = List(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

scala> list.mkString(",")
res60: String = Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday

  樊旭升成新的列表

scala> list
res63: List[String] = List(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

scala> list.reverse
res64: List[String] = List(Sunday, Saturday, Friday, Thursday, Wednesday, Tuesday, Monday)

  按照字母递增顺序

scala> list
res65: List[String] = List(Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)

scala> list.sortWith(_.compareTo(_)<0)
res66: List[String] = List(Friday, Monday, Saturday, Sunday, Thursday, Tuesday, Wednesday)

  

posted @ 2019-10-21 14:13  console.log('宇航')  阅读(438)  评论(0编辑  收藏  举报