1、数组
- Array可以定义不可变数组,这里的不可变指的是长度不可变
scala> var arr = Array("233", "33") arr: Array[String] = Array(233, 33) scala> arr(1) = "22" scala> println(arr(1)) 22 scala> arr(2) java.lang.ArrayIndexOutOfBoundsException: 2 ... 48 elided
可以指定数组的具体类型,也可以不指定,Scala会自行判定
scala> val arrStr = Array("scala","spark") arrStr: Array[String] = Array(scala, spark) scala> val arrInt = new Array[Int](10) arrInt: Array[Int] = Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0) scala> val arrStr = Array("scala",1) arrStr: Array[Any] = Array(scala, 1)
遍历数组
scala> var arr = Array("string","spark") arr: Array[String] = Array(string, spark) scala> for(i <- 0 until arr.length)print(arr(i) + " ") string spark scala> for(ex <- arr)println(ex) string spark
转换为ArrayBuffer
scala> var arr = Array("233","23333") arr: Array[String] = Array(233, 23333) scala> var arr1 = arr.toBuffer arr1: scala.collection.mutable.Buffer[String] = ArrayBuffer(233, 23333) scala> arr1 += "222" res1: scala.collection.mutable.Buffer[String] = ArrayBuffer(233, 23333, 222)
- ArrayBuffer
ArrayBuffer是可变数组,使用前需要显示导入包
scala> import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer scala> var arrBufInt = ArrayBuffer[Int]() arrBufInt: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
+=在尾端添加元素
scala> arrBufInt+= 1
res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1)
在尾端添加多个元素
scala> arrBufInt += (2,3,4,5)
res4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5)
++=操作符可以追加任何集合
scala> var arrBufInt2 = ArrayBuffer[Int]() //创建一个新的ArrayBuffer arrBufInt2: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer() scala> arrBufInt2 += (3,4,5) //从尾端添加3,4,5 res5: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(3, 4, 5) scala> arrBufInt ++= arrBufInt2 //从尾端添加一个ArrayBuffer res6: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 5, 3, 4, 5) scala> for(i <- 0 until arrBufInt.length)print(arrBufInt(i) + " ") 1 2 3 4 5 3 4 5
ArrayBuffer的一些方法
scala> arrBufInt.trimEnd(2) //移除最后两个元素 scala> for(i <- 0 until arrBufInt.length)print(arrBufInt(i) + " ") 1 2 3 4 5 3 4 5 6 scala> arrBufInt.remove(2) //移除索引号为2的元素 res13: Int = 3 scala> for(i <- 0 until arrBufInt.length)print(arrBufInt(i) + " ") 1 2 4 5 3 4 5 6 scala> arrBufInt.remove(2,4) //移除从索引号为2开始的4个元素 scala> for(i <- 0 until arrBufInt.length)print(arrBufInt(i) + " ") 1 2 5 6 scala> var arry = arrBufInt.toArray //转换为Array arry: Array[Int] = Array(1, 2, 5, 6)
2、Map
Scala映射就是键值对的集合Map,默认情况下,Scala使用不可变的映射。如果想使用可变集,必须明确地导入scala.collection.mutable.Map
- 构建不可变映射
val map = Map("name" -> "steve","age" -> "12","sex" -> "man") println(map("name")) //获取key为name的值 println("map中是否存在name:" + map.contains("name")) //判断map中是否有name作为key的键值对 println(map.getOrElse("age", "24")) //若map中有age作为key的键值对那么返回对应的value,若不存在就返回24 println(map.getOrElse("ages", "24")) //遍历map map.keys.foreach{i => print("key = " + i) println(" value = " + map(i))}
输出:
steve map中是否存在name:true 12 24 key = age value = 12 key = name value = steve key = sex value = man
- 构造可变映射
val map = scala.collection.mutable.Map("name" -> "steve","age" -> "12") map += ("sex" -> "boy")//添加键值对 println("map中是否存在sex:" + map.contains("sex")) map -= ("sex" )//删除键值对 println("map中是否存在sex:" + map.contains("sex")) //遍历map for((k, v) <- map){println(k + " " + v)} //遍历ma的键值 for(key <- map.keySet){println(key)}
输出:
map中是否存在sex:true map中是否存在sex:false age 12 name steve age name
3、Tuple元组
Tuple元组就是由两个小括号包住可以存放任意类型的数据集合
val t = ("1",2,3.0) t: (String, Int, Double) = (1,2,3.0) val t = new Tuple3(1, 3.14, "Fred") t: (Int, Double, String) = (1,3.14,Fred)
元组的实际类型取决于它的元素的类型,要注意的是元组的长度是不可变的,目前 Scala 支持的元组最大长度为 22。对于更大长度你可以使用集合,或者扩展元组
scala> t._1 res5: Int = 1 scala> t._2 res6: Double = 3.14 scala> t._3 res7: String = Fred
这样就能访问元组的元素
scala> t.productIterator.foreach{ i =>println("Value = " + i )} Value = 1 Value = 3.14 Value = Fred
使用 Tuple.productIterator() 方法来迭代输出元组的所有元素