视频下载地址:http://yunpan.cn/cmu9xQa6HRIRY 访问密码 2e0e
大数据微信公众账号:DT_Spark
/*
*定长数组
*/
val s = Array("Hello","World")
//> s : Array[String] = Array(Hello, World)
s(0)="GoleBye"
s
//> res0: Array[String] = Array(GoleBye, World)
//int类型数组,是默认0
val nums = new Array[Int](10)
//> nums : Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
//string类型数组,默认是null
//> a : Array[String] = Array(null, null, null, null, null, null, null, null, null, null)
/*
*可变数组
*/
import scala.collection.mutable.ArrayBuffer
val b = ArrayBuffer[Int]()
//> b : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
//把1添加到b数组里
b += 1
//> res1: com.wy.scala.ScalaInAction.b.type = ArrayBuffer(1)
//把数组1235添加到数组里
b += (1,2,3,5)
//> res2: com.wy.scala.ScalaInAction.b.type = ArrayBuffer(1, 1, 2, 3, 5)
//把数组添加到b数组里
b ++= Array(13,18,21)
//> res3: com.wy.scala.ScalaInAction.b.type = ArrayBuffer(1, 1, 2, 3, 5, 13, 18,21)
b
//> res4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 5,13, 18, 21)
//去掉最后一个
b.trimEnd(1)
b
//> res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 1, 2, 3, 5, 13, 18)
//去掉第一个
b.trimStart(1)
b
//> res6: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 5, 13, 18)
//在1位标下插入11111111
b.insert(1,11111111)
b
//> res7: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 11111111, 2, 3, 5, 13, 18)
//在3的位标下插入3333333,4,4444444
b.insert(3,33,4,44)
b
//> res8: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 11111111, 2, 33, 4, 44, 3, 5, 13, 18)
b.size
//> res9: Int = 10
//删除索引为2的数据
b.remove(2)
//> res10: Int = 2
//> res11: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 11111111, 33, 4, 44, 3, 5, 13, 18)
//删除索引为2的后面3位数据
b.remove(2,3)
b
//> res12: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 11111111, 3, 5, 13, 18)
val c = b.toArray
//c是不可变的
c+=1 //错误
yield关键字
val c = Array(2,3,5,7)
//> c : Array[Int] = Array(2, 3, 5, 7)
val result = for(elem <- c) yield elem *2
//> result : Array[Int] = Array(4, 6, 10, 14)
for(elem <- c if elem % 2 == 0)yield elem * 2
//> res14: Array[Int] = Array(4)
//生产模式下一般这样写
c.filter(_ %2 == 0).map(_*2)
//> res15: Array[Int] = Array(4)
求和
Array(1,2,3,4).sum
//> res16: Int = 10
排序
//不是按长度排,是按字母顺序
val cc = ArrayBuffer("m","1","nittle","anglebaby").max
//> cc : String = nittle
//cc
ArrayBuffer("su","1","little","anglebaby").min
//> res17: String = 1
//排序
val d = Array(1,7,3,9)
//> d : Array[Int] = Array(1, 7, 3, 9)
//> res18: Array[Int] = Array(1, 3, 7, 9)
//quick排序
scala.util.Sorting.quickSort(d)
d
//> res19: Array[Int] = Array(1, 3, 7, 9)
mkString
//mkString方法可以串连数组或集合的元素
d.mkString(" and ")
//> res20: String = 1 and 3 and 7 and 9
d.mkString("<",".",">")
//> res21: String = <1.3.7.9>
二维数组:
//二维数组
val ay = Array.ofDim[Double](3,4)
//> ay : Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0, 0.0))
ay(2)(2) = 27.2
ay
//> res22: Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0, 0.0), Array(0.0, 0.0, 0.0, 0.0), Array(0.0, 0.0, 27.2, 0.0))
val x = new Array[Array[Int]](10)
//> x : Array[Array[Int]] = Array(null, null, null, null, null, null, null, null, null, null)
for(i <- 0 until x.length)
x(i) = new Array[Int](i+1)
x
//> res23: Array[Array[Int]] = Array(Array(0), Array(0, 0), Array(0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0 , 0), Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
so easy …