欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《Scala CookBook》Chapter11

程序员文章站 2022-06-15 12:05:52
...

《Scala CookBook》Chapter11

List
2.1scala的List类是不可变的,大小及其中元素都不能修改。由链表实现,包含常用的head,tail,isEmpty方法。大多数在List上的操作都会涉及递归算法,将列表且分为头部和尾部。
现在有一个需求:如果一个链表的顺序是5,4,3,2,1。那么怎么才能不改变链表的情况下逆序打印出1,2,3,4,5.
答:可以使用递归解决这个问题。
(1)定义一个打印方法A,接收的参数是链表中的头结点N1。
(2)传入链表头结点H1
(3)将节点5之后的(4,3,2,1)作为一个新的链表,并将这个新链表的头结点H2传入方法A
(4)递归结束。
(5)方法A中的后面是打印出信息

2.2想使用可变列表(相对于索引序列,是线性序列),但是List是不可变的,可以使用ListBuffer,在必要时可以将ListBuffer转换为List。

2.3List是不可变的,如果创建要经常改变的列表,推荐使用ListBuffer.

2.4List是不可变的,不能从中删除元素,但是可以过滤掉不想要的元素,然后将结果赋给一个新的变量。

2.5合并或者连接列表
01.++
02.concat
03.:::方法

Stream
11.6使用List的懒惰版本,Stream
Stream和List很像,不同之处在于它的元素是惰性计算的【只有元素被访问时才会计算】,和视图创建惰性版本的集合类似。
1.构建Stream的方法
使用#::方法构建,表达式的结尾用Stream.empty代替Nil
val stream = 1 #:: 2 #:: 3 #:: 4 #:: Stream.empty
stream: scala.collection.immutable.Stream[Int] = Stream(1, ?)
显示流开始于数字1,结尾用?表示。这是因为流的结尾还没有执行。
2.对REPL的显示进行说明
3.访问流的头部元素和尾部元素

Array
11.7创建和更新数组【Array】的不同方式
01.可以创建具有初始值的数组。scala能够隐式推断数组类型。示例如下:
val arr = Array(“apple”,”google”,”alibaba”)
val arr = ArrayInt
val fruits = new ArrayString//定义一个数组初始大小和类型,随后填充。
02.Array类:数组是可变的,有序的集合。数组中的元素是可以修改的。数组是一种索引序列集合,根据索引位置直接而且快速地访问和修改值。

ArrayBuffer
11.8创建大小可变的数组(ArrayBuffer)
01.Array的内容是可变的,但是其大小不能改变。但是ArrayBuffer内容可变,大小亦可变。
02.往ArrayBuffer中添加元素的各种方法
03.需要导入包scala.collection.mutable.ArrayBuffer

11.9删除Array和ArrayBuffer的元素
01.从ArrayBuffer中删除元素的各种方法-=/–=/remove/clear
-=
–=
remove
clear
02.Array数组大小不能改变,所以不能直接删除元素。但是可以将元素从一个数组中过滤出去,然后将结果赋值给新的数组。

11.10数组排序
1.sorted方法可以对具有Double,Float,Int和任何隐式scala.math.Ordering的其它类型集合进行排序。
sorted
2.如果想使用sorted方法排序Person类,只需要把Ordered特质混入Person类。
3.sortWith可以传递函数【函数由简单—>复杂】;sortWith可以对没有Ordering的类型进行排序。例如:
class Person(var name:String){
override def toString = name
}

scala> var a = Array("apple","banana")
a: Array[String] = Array(apple, banana)

scala> a = a.take(2)
a: Array[String] = [Ljava.lang.String;@70abf9b0

scala> a
res0: Array[String] = Array(apple, banana)

scala> val fruits: Array[String] = Array("cherry","apple","banana")
fruits: Array[String] = Array(cherry, apple, banana)

scala>   val res0 = scala.util.Sorting.quickSort(fruits)

scala> res0

scala> scala.util.Sorting.quickSort(fruits)

scala> val fruits = Array("cherry","apple","banana")
fruits: Array[String] = Array(cherry, apple, banana)

scala> scala.util.Sorting.quickSort(fruits)

scala> fruits
res4: Array[String] = Array(apple, banana, cherry)

scala> val stream = 1 #:: 2 #:: 3 #:: 4 #:: Stream.empty
stream: scala.collection.immutable.Stream[Int] = Stream(1, ?)

scala> val arr = ArrayBuffer("apple","google","alibaba")
<console>:11: error: not found: value ArrayBuffer
       val arr = ArrayBuffer("apple","google","alibaba")
                 ^

scala> import scala.util.collection.mutalbe.ArrayBuffer
<console>:11: error: object collection is not a member of package util
       import scala.util.collection.mutalbe.ArrayBuffer
                         ^

scala> import scala.collection.mutalbe.ArrayBuffer
<console>:11: error: object mutalbe is not a member of package collection
       import scala.collection.mutalbe.ArrayBuffer
                               ^

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val arr = ArrayBuffer("apple","google","alibaba")
arr: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(apple, google, alibaba)

scala> arr(1)
res5: String = google

scala> arr(2)
res6: String = alibaba

scala> arr(3)
java.lang.IndexOutOfBoundsException: 3
  at scala.collection.mutable.ResizableArray$class.apply(ResizableArray.scala:43)
  at scala.collection.mutable.ArrayBuffer.apply(ArrayBuffer.scala:48)
  ... 32 elided

scala> arr(3) = "EnMonster"
java.lang.IndexOutOfBoundsException: 3
  at scala.collection.mutable.ResizableArray$class.update(ResizableArray.scala:48)
  at scala.collection.mutable.ArrayBuffer.update(ArrayBuffer.scala:48)
  ... 32 elided

scala> arr += "EnMonster"
res9: arr.type = ArrayBuffer(apple, google, alibaba, EnMonster)

scala> arr ++= Seq("xiaomi","HuaWei")
res10: arr.type = ArrayBuffer(apple, google, alibaba, EnMonster, xiaomi, HuaWei)

scala> arr --= Seq("xiaomi","apple")
res11: arr.type = ArrayBuffer(google, alibaba, EnMonster, HuaWei)

scala> arr -=  "EnMonster"
res12: arr.type = ArrayBuffer(google, alibaba, HuaWei)

scala> arr.length
res13: Int = 3

scala> arr.remove(0)
res14: String = google

scala> arr
res15: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(alibaba, HuaWei)

scala> var a = Array("apple","banana")
a: Array[String] = Array(apple, banana)

scala> a = a.take(2)
a: Array[String] = [Ljava.lang.String;@70abf9b0

scala> a
res0: Array[String] = Array(apple, banana)

scala> val fruits: Array[String] = Array("cherry","apple","banana")
fruits: Array[String] = Array(cherry, apple, banana)

scala>   val res0 = scala.util.Sorting.quickSort(fruits)

scala> res0

scala> scala.util.Sorting.quickSort(fruits)

scala> val fruits = Array("cherry","apple","banana")
fruits: Array[String] = Array(cherry, apple, banana)

scala> scala.util.Sorting.quickSort(fruits)

scala> fruits
res4: Array[String] = Array(apple, banana, cherry)

scala> val stream = 1 #:: 2 #:: 3 #:: 4 #:: Stream.empty
stream: scala.collection.immutable.Stream[Int] = Stream(1, ?)

scala> val arr = ArrayBuffer("apple","google","alibaba")
<console>:11: error: not found: value ArrayBuffer
       val arr = ArrayBuffer("apple","google","alibaba")
                 ^

scala> import scala.util.collection.mutalbe.ArrayBuffer
<console>:11: error: object collection is not a member of package util
       import scala.util.collection.mutalbe.ArrayBuffer
                         ^

scala> import scala.collection.mutalbe.ArrayBuffer
<console>:11: error: object mutalbe is not a member of package collection
       import scala.collection.mutalbe.ArrayBuffer
                               ^

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val arr = ArrayBuffer("apple","google","alibaba")
arr: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(apple, google, alibaba)

scala> arr(1)
res5: String = google

scala> arr(2)
res6: String = alibaba

scala> arr(3)
java.lang.IndexOutOfBoundsException: 3
  at scala.collection.mutable.ResizableArray$class.apply(ResizableArray.scala:43)
  at scala.collection.mutable.ArrayBuffer.apply(ArrayBuffer.scala:48)
  ... 32 elided

scala> arr(3) = "EnMonster"
java.lang.IndexOutOfBoundsException: 3
  at scala.collection.mutable.ResizableArray$class.update(ResizableArray.scala:48)
  at scala.collection.mutable.ArrayBuffer.update(ArrayBuffer.scala:48)
  ... 32 elided

scala> arr += "EnMonster"
res9: arr.type = ArrayBuffer(apple, google, alibaba, EnMonster)

scala> arr ++= Seq("xiaomi","HuaWei")
res10: arr.type = ArrayBuffer(apple, google, alibaba, EnMonster, xiaomi, HuaWei)

scala> arr --= Seq("xiaomi","apple")
res11: arr.type = ArrayBuffer(google, alibaba, EnMonster, HuaWei)

scala> arr -=  "EnMonster"
res12: arr.type = ArrayBuffer(google, alibaba, HuaWei)

scala> arr.length
res13: Int = 3

scala> arr.remove(0)
res14: String = google

scala> arr
res15: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(alibaba, HuaWei)

scala>