集合类之List(九)

版权声明:菲立思教育 https://blog.csdn.net/cold_wolfie/article/details/82533931

简介

  Scala 列表类似于数组,它们所有元素的类型都相同,但是它们也有所不同:列表是不可变的,值一旦被定义了就不能改变;其次,列表 具有递归的结构(也就是链接表结构)而数组不是。


// 字符串列表
val site: List[String] = List("Sina", "Google", "Baidu")

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

// 空列表
val empty: List[Nothing] = List()

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

组成部分

  构造列表的两个基本单位是Nil和::。Nil可以表示一个空列表。

// ::连接顺序为从右向左

// 字符串列表
val site = "Sina" :: ("Google" :: ("Baidu" :: Nil))

// 整型列表
val nums = 1 :: (2 :: (3 :: (4 :: Nil)))

// 空列表
val empty = Nil

// 二维列表
val dim = (1 :: (0 :: (0 :: Nil))) ::
  (0 :: (1 :: (0 :: Nil))) ::
  (0 :: (0 :: (1 :: Nil))) :: Nil

常用操作

//+:  在列表的头部添加一个元素
//:+  在列表的尾部添加一个元素
//::  在列表的头部添加一个元素

//判断是否为空
scala> nums.isEmpty
res108: Boolean = false

//取第一个无素
scala> nums.head
res109: Int = 1

//取除第一个元素外剩余的元素,返回的是列表
scala> nums.tail
res114: List[Int] = List(2, 3, 4)

//取列表第二个元素
scala> nums.tail.head
res115: Int = 2

//插入排序算法实现
def isort(xs: List[Int]): List[Int] =
if (xs.isEmpty) Nil
else insert(xs.head, isort(xs.tail))

def insert(x: Int, xs: List[Int]): List[Int] =
if (xs.isEmpty || x <= xs.head) x :: xs
else xs.head :: insert(x, xs.tail)

//List连接操作
scala> List(1,2,3):::List(4,5,6)
res116: List[Int] = List(1, 2, 3, 4, 5, 6)

//取除最后一个元素外的元素,返回的是列表
scala> nums.init
res117: List[Int] = List(1, 2, 3)

//取列表最后一个元素
scala> nums.last
res118: Int = 4

//列表元素倒置
scala> nums.reverse
res119: List[Int] = List(4, 3, 2, 1)

//一些好玩的方法调用
scala> nums.reverse.reverse==nums
res120: Boolean = true

scala> nums.reverse.init
res121: List[Int] = List(4, 3, 2)

scala> nums.tail.reverse
res122: List[Int] = List(4, 3, 2)

//丢弃前n个元素
scala> nums drop 3
res123: List[Int] = List(4)

scala> nums drop 1
res124: List[Int] = List(2, 3, 4)

//获取前n个元素
scala> nums take 1
res125: List[Int] = List(1)

scala> nums.take(3)
res126: List[Int] = List(1, 2, 3)

//将列表进行分割
scala> nums.splitAt(2)
res127: (List[Int], List[Int]) = (List(1, 2),List(3, 4))

//前一个操作与下列语句等同
scala> (nums.take(2),nums.drop(2))
res128: (List[Int], List[Int]) = (List(1, 2),List(3, 4))

//Zip操作
scala> val nums=List(1,2,3,4)
nums: List[Int] = List(1, 2, 3, 4)

scala> val chars=List('1','2','3','4')
chars: List[Char] = List(1, 2, 3, 4)

//返回的是List类型的元组(Tuple)
scala> nums zip chars
res130: List[(Int, Char)] = List((1,1), (2,2), (3,3), (4,4))

//List toString方法
scala> nums.toString
res131: String = List(1, 2, 3, 4)

//List mkString方法
scala> nums.mkString
res132: String = 1234

//转换成数组
scala> nums.toArray
res134: Array[Int] = Array(1, 2, 3, 4)

伴生对象方法

//apply方法
scala>  List.apply(1, 2, 3)
res139: List[Int] = List(1, 2, 3)

//range方法,构建某一值范围内的List
scala>  List.range(2, 6)
res140: List[Int] = List(2, 3, 4, 5)

//步长为2
scala>  List.range(2, 6,2)
res141: List[Int] = List(2, 4)

//步长为-1
scala>  List.range(2, 6,-1)
res142: List[Int] = List()

scala>  List.range(6,2 ,-1)
res143: List[Int] = List(6, 5, 4, 3)

//构建相同元素的List
scala> List.make(5, "hey")
res144: List[String] = List(hey, hey, hey, hey, hey)

//unzip方法
scala> List.unzip(res145)
res146: (List[Int], List[Char]) = (List(1, 2, 3, 4),List(1, 2, 3, 4))

//list.flatten,将列表平滑成第一个无素
scala> val xss =
     | List(List('a', 'b'), List('c'), List('d', 'e'))
xss: List[List[Char]] = List(List(a, b), List(c), List(d, e))
scala> xss.flatten
res147: List[Char] = List(a, b, c, d, e)

//列表连接
scala> List.concat(List('a', 'b'), List('c'))
res148: List[Char] = List(a
, b, c)

遍历操作

object Test6 extends App() {
  /**
   * 单个集合的各种遍历方式
   */
   val lst = List(1,2,3,4,5);
   print("foreach遍历:")
   lst.foreach { x => print(x+",")}  //foreach遍历,这个是传统遍历,新手不熟无奈之下可以用它
   println("")

   var temp = lst.map { x => x+1 }   //遍历,与foreach的区别是返回值为List【B】
   println("map遍历:"+temp.mkString(","));


   var temp1 = lst.reduceLeft((sum,i)=>sum +i) //遍历,返回值类型是一个与集合相同的Int
    println("reduce遍历返回Int:"+temp1);

   var temp2 = lst.foldLeft(List[Int]())((x,y)=>y::x); //遍历,返回值是自定义类型
   //ps fold类函数还可以改成 :\ ,/:的形式,代码精简了不少,但是可读性却减低了例如
    println("foldLeft遍历返回自定义类型:"+temp2.mkString(","));

    var temp3=(   List[Int]() /: lst){(m,c)=>c::m} //遍历,实现反转
     println("foldLeft遍历实现反转:"+temp2.mkString(","));

}

列表缓存(ListBuffer)

ListBuffer特点

  • ListBuffer长度可变,元素可变。
  • 使用ListBuffer,可以在列表尾部添加对象。
  • 使用ListBuffer替代List另一个理由是避免栈溢出风险。

简单实例

val buf: ListBuffer[Int] = new ListBuffer[Int]
//往后添加
buf += 1
buf += 2

//前缀添加
val buf2 = 3 +: buf
println(buf2.toString())

//ListBuffer转List
println(buf2.toList.toString())

val listBuffer = scala.collection.mutable.ListBuffer(1,2,3)
listBuffer.append(5)//添加一个元素
listBuffer += 8//添加一个元素
listBuffer -= 2//移除第二个元素
println("listBuffer: " + listBuffer)

猜你喜欢

转载自blog.csdn.net/cold_wolfie/article/details/82533931