本节主要内容
- 数组操作实战
- 列表List操作实战
数组操作实战
1 定长数组
scala> val numberArray=new Array[Int](10)
numberArray: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
scala> val strArray=new Array[String](10)
strArray: Array[String] = Array(null, null, null, null, null, null, null, null,
null, null)
scala> strArray(0)="First Element"
scala> strArray
res62: Array[String] = Array(First Element, null, null, null, null, null, null,
null, null, null)
scala> val strArray2=Array("First","Second")
strArray2: Array[String] = Array(First, Second)
Scala中的Array以Java中的Array方式实现
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
2 变长数组 ArrayBuffer
//要使用ArrayBuffer,先要引入scala.collection.mutable.ArrayBuffer
scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer
//创建String类型ArrayBuffer数组缓冲
scala> val strArrayVar=ArrayBuffer[String]()
strArrayVar: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer()
//+=意思是在尾部添加元素
scala> strArrayVar+="Hello"
res63: strArrayVar.type = ArrayBuffer(Hello)
//+=后面还可以跟多个元素的集合
//注意操作后的返回值
scala> strArrayVar+=("World","Programmer")
res64: strArrayVar.type = ArrayBuffer(Hello, World, Programmer)
//显示完整数组内容
scala> strArrayVar
res65: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(Hello, World,
Programmer)
//++=用于向数组中追加内容,++=右侧可以是任何集合
//追加Array数组
scala> strArrayVar++=Array("Wllcome","To","XueTuWuYou")
res66: strArrayVar.type = ArrayBuffer(Hello, World, Programmer, Wllcome, To, Xue
TuWuYou)
//追加List
scala> strArrayVar++=List("Wellcome","To","XueTuWuYou")
res67: strArrayVar.type = ArrayBuffer(Hello, World, Programmer, Wllcome, To, Xue
TuWuYou, Wellcome, To, XueTuWuYou)
//删除末尾n个元素
scala> strArrayVar.trimEnd(3)
scala> strArrayVar
res69: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(Hello, World,
Programmer, Wllcome, To, XueTuWuYou)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
//创建整型数组缓冲
scala> var intArrayVar=ArrayBuffer(1,1,2)
intArrayVar: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)
//在数组索引为0的位置插入元素6
scala> intArrayVar.insert(0,6)
scala> intArrayVar
res72: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(6, 1, 1, 2)
//在数组索引为0的位置插入元素7,8,9
scala> intArrayVar.insert(0,7,8,9)
scala> intArrayVar
res74: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(7, 8, 9, 6, 1, 1,2)
//从索引0开始,删除4个元素
scala> intArrayVar.remove(0,4)
scala> intArrayVar
res77: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2)
//转成定长数组
scala> intArrayVar.toArray
res78: Array[Int] = Array(1, 1, 2)
//将定长数组转成ArrayBuffer
scala> res78.toBuffer
res80: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 1, 2)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
3 数组的遍历
//to
scala> for(i <- 0 to intArrayVar.length-1) println("Array Element: " +intArrayVar(i))
Array Element: 1
Array Element: 1
Array Element: 2
//until
scala> for(i <- 0 until intArrayVar.length) println("Array Element: " +intArrayVar(i))
Array Element: 1
Array Element: 1
Array Element: 2
//数组方式(推荐使用)
scala> for(i <- intArrayVar) println("Array Element: " + i)
Array Element: 1
Array Element: 1
Array Element: 2
//步长为2
scala> for(i <- 0 until (intArrayVar.length,2)) println("Array Element: " +intA
rrayVar(i))
Array Element: 1
Array Element: 2
//倒序输出
scala> for( i<- (0 until intArrayVar.length).reverse) println("Array Element: "+
intArrayVar(i))
Array Element: 2
Array Element: 1
Array Element: 1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
4 数组转换
scala> var intArrayVar2=for(i <- intArrayVar) yield i*2
intArrayVar2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 2, 4)
scala> var intArrayNoBuffer=Array(1,2,3)
intArrayNoBuffer: Array[Int] = Array(1, 2, 3)
scala> var intArrayNoBuffer2=for(i <- intArrayNoBuffer) yield i*2
intArrayNoBuffer2: Array[Int] = Array(2, 4, 6)
scala> var intArrayNoBuffer2=for(i <- intArrayNoBuffer if i>=2) yield i*2
intArrayNoBuffer2: Array[Int] = Array(4, 6)
5 数组操作中的常用算法
//定义一个整型数组
scala> val intArr=Array(1,2,3,4,5,6,7,8,9,10)
intArr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
//求和
scala> intArr.sum
res87: Int = 55
//求最大值
scala> intArr.max
res88: Int = 10
scala> ArrayBuffer("Hello","Hell","Hey","Happy").max
res90: String = Hey
//求最小值
scala> intArr.min
res89: Int = 1
//toString()方法
scala> intArr.toString()
res94: String = [I@141aba8
//mkString()方法
scala> intArr.mkString(",")
res96: String = 1,2,3,4,5,6,7,8,9,10
scala> intArr.mkString("<",",",">")
res97: String = <1,2,3,4,5,6,7,8,9,10>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
6 ArrayBuffer Scaladoc解析
初学者在查看sacaladoc时常常会感到困惑,不用担心,随着学习的深入,api文档中的内容将逐渐清晰
下面给出两个示例:
++=方法传入的参数类型是TraversableOnce Trait的子类,它返回的是更新好的ArrayBuffer
dropWhile传入的是一个函数,该函数返回值是布尔类型,dropWhile反回的是操作后的ArrayBuffer
7 多维数组
通过数组的数组实现多维数组的定义:
scala> var multiDimArr=Array(Array(1,2,3),Array(2,3,4))
multiDimArr: Array[Array[Int]] = Array(Array(1, 2, 3), Array(2, 3, 4))
scala> multiDimArr(0)(2)
res99: Int = 3
scala> for(i <- multiDimArr) println( i.mkString(","))
1,2,3
2,3,4
列表List操作实战
1 List类型定义及List的特点
//字符串类型List
scala> val fruit=List("Apple","Banana","Orange")
fruit: List[String] = List(Apple, Banana, Orange)
//前一个语句与下面语句等同
scala> val fruit=List.apply("Apple","Banana","Orange")
fruit: List[String] = List(Apple, Banana, Orange)
//数值类型List
scala> val nums=List(1,2,3,4,5)
nums: List[Int] = List(1, 2, 3, 4, 5)
//多重List,List的子元素为List
scala> val diagMatrix=List(List(1,0,0),List(0,1,0),List(0,0,1))
diagMatrix: List[List[Int]] = List(List(1, 0, 0), List(0, 1, 0), List(0, 0, 1))
//遍历List
scala> for (i <- nums) println("List Element: "+i)
List Element: 1
List Element: 2
List Element: 3
List Element: 4
List Element: 5
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
不难看出,List与Array有着诸多的相似之处,但它们有两个明显的区别:
1 List一但创建,其值不能被改变
如前面的nums,改变其值的话,编译器会报错
scala> nums(3)=4
<console>:10: error: value update is not a member of List[Int]
nums(3)=4
^
2 List具有递归结构(Recursive Structure),例如链表结构
List类型和其它类型集合一样,它具有协变性(Covariant),即对于类型S和T,如果S是T的子类型,则List[S]也是List[T]的子类型
例如
scala> var listStr:List[Object]=List("This","Is","Covariant","Example")
listStr: List[Object] = List(This, Is, Covariant, Example)
scala> var listStr=List()
listStr: List[Nothing] = List()
scala> var listStr:List[String]=List()
listStr: List[String] = List()
2 List常用构造方法
scala> val nums = 1 :: (2 :: (3 :: (4 :: Nil)))
nums: List[Int] = List(1, 2, 3, 4)
scala> val nums=1::2::3::4::Nil
nums: List[Int] = List(1, 2, 3, 4)
3 List常用操作
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)
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)
scala> nums drop 3
res123: List[Int] = List(4)
scala> nums drop 1
res124: List[Int] = List(2, 3, 4)
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))
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)
scala> nums zip chars
res130: List[(Int, Char)] = List((1,1), (2,2), (3,3), (4,4))
scala> nums.toString
res131: String = List(1, 2, 3, 4)
scala> nums.mkString
res132: String = 1234
scala> nums.toArray
res134: Array[Int] = Array(1, 2, 3, 4)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- List伴生对象方法
scala> List.apply(1, 2, 3)
res139: List[Int] = List(1, 2, 3)
scala> List.range(2, 6)
res140: List[Int] = List(2, 3, 4, 5)
scala> List.range(2, 6,2)
res141: List[Int] = List(2, 4)
scala> List.range(2, 6,-1)
res142: List[Int] = List()
scala> List.range(6,2 ,-1)
res143: List[Int] = List(6, 5, 4, 3)
scala> List.make(5, "hey")
res144: List[String] = List(hey, hey, hey, hey, hey)
scala> List.unzip(res145)
res146: (List[Int], List[Char]) = (List(1, 2, 3, 4),List(1, 2, 3, 4))
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)