Scala入门到精通——第三节 Array、List

本节主要内容

  1. 数组操作实战
  2. 列表List操作实战

数组操作实战

1 定长数组

//定义一个长度为10的数值数组
scala> val numberArray=new Array[Int](10)
numberArray: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
//定义一个长度为10的String类型数组
scala> val strArray=new Array[String](10)
strArray: Array[String] = Array(null, null, null, null, null, null, null, null,
null, null)

//可以看出:复杂对象类型在数组定义时被初始化为null,数值型被初始化为0

//数组元素赋值
scala> strArray(0)="First Element"
//需要注意的是,val strArray=new Array[String](10)
//这意味着strArray不能被改变,但数组内容是可以改变的
scala> strArray
res62: Array[String] = Array(First Element, null, null, null, null, null, null,
null, null, null)


//另一种定长数组定义方式
//这种调用方式其实是调用其apply方法进行数组创建操作
scala> val strArray2=Array("First","Second")
strArray2: Array[String] = Array(First, Second)

Scala中的Array以Java中的Array方式实现

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)
//创建整型数组缓冲
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)

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

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>

6 ArrayBuffer Scaladoc解析

初学者在查看sacaladoc时常常会感到困惑,不用担心,随着学习的深入,api文档中的内容将逐渐清晰
下面给出两个示例:
++=方法传入的参数类型是TraversableOnce Trait的子类,它返回的是更新好的ArrayBuffer
Scala入门到精通——第三节 Array、List

dropWhile传入的是一个函数,该函数返回值是布尔类型,dropWhile反回的是操作后的ArrayBuffer
Scala入门到精通——第三节 Array、List

7 多维数组

通过数组的数组实现多维数组的定义:

//定义2行3列数组
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)

//多重ListList的子元素为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

不难看出,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)

//空的List,其类型为Nothing,Nothing在Scala的继承层次中的最低层
//,即Nothing是任何Scala其它类型如String,Object等的子类
scala> var listStr=List()
listStr: List[Nothing] = List()

scala> var listStr:List[String]=List()
listStr: List[String] = List()

2 List常用构造方法

//采用::及Nil进行列表构建
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)

//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)
  1. List伴生对象方法
//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)

添加公众微信号,可以了解更多最新Spark、Scala相关技术资讯
Scala入门到精通——第三节 Array、List

上一篇:Akka并发编程——第五节:Actor模型(四)


下一篇:SparkSQL(Spark-1.4.0)实战系列(三)——SparkSQL应用案例