您好,登錄后才能下訂單哦!
定長數組
scala> val arr1 = new Array[String](8)
arr1: Array[String] = Array(null, null, null, null, null, null, null, null)
scala> arr1
res5: Array[String] = Array(null, null, null, null, null, null, null, null)
scala> println(arr1)
[Ljava.lang.String;@1254a3cd
scala> println(arr1.toBuffer)
ArrayBuffer(null, null, null, null, null, null, null, null)
scala> val arr2 = Array("java", "scala", "python")
arr2: Array[String] = Array(java, scala, python)
scala> println(arr2(0))
java
邊長數組
scala> val arr3 = ArrayBuffer[Int]()
scala> arr3 +=1
scala> arr3 ++= Array(5,6)
scala> arr3 ++= ArrayBuffer(7,8)
#在0的位置加入-1
scala> arr3.insert(0, -1)
#在第2的位置,加入2
scala> arr3.remove(2,2)
scala> val arr = Array(1,2,3,4,5,6,7,8,9)
arr: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
scala> for(i <- arr) println(i)
#遍歷數組
for(i <- 0 until arr.length) println(i)
for(i <- (0 until arr.length).reverse) println(i)
#把原來數組的值重新計算,生成一個新的數組
scala> val res = for(i <- arr) yield i* 10
res: Array[Int] = Array(10, 20, 30, 40, 50, 60, 70, 80, 90)
#數組所有的和
scala> arr.sum
res22: Int = 45
#數組中最大的值
scala> arr.max
res23: Int = 9
數組排序
scala> arr.sorted
#數組相加
scala> val arr4 = arr2 ++ arr3
arr4: Array[Int] = Array(24, 25, 255, 2, 21, 253)
scala> val t = ("scala",1,3.14,("spark",1))
t: (String, Int, Double, (String, Int)) = (scala,1,3.14,(spark,1))
#取元組里面第一個值,(元組下標從1開始的)
scala> t._1
res29: String = scala
#array 轉成map
scala> val arr = Array(("t",2),("n",3),("x",4))
arr: Array[(String, Int)] = Array((t,2), (n,3), (x,4))
scala> arr.toMap
res30: scala.collection.immutable.Map[String,Int] = Map(t -> 2, n -> 3, x -> 4)
#拉鏈操作,把2個Array轉成元組
scala> val arr1 = Array("tingting","c","d")
arr1: Array[String] = Array(tingting, c, d)
scala> val arr2 = Array(24,25,255)
arr2: Array[Int] = Array(24, 25, 255)
scala> arr1 zip arr2
res31: Array[(String, Int)] = Array((tingting,24), (c,25), (d,255))
import scala.collection.immutable.HashSet
val set1 = new HashSet[Int]()
val set2 = set1 +1
2個set組合成一個新的set
val set3 = set2 ++ Set(2,3,4)
#操作set,增加/刪除元素
set1 += 1
set1 -= 4
scala> val map2 = Map(("scala",1), ("java",2), ("python",3))
map2: scala.collection.immutable.Map[String,Int] = Map(scala -> 1, java -> 2, python -> 3)
scala> val map1 = Map("scala"->1,"java"->2, "python"->3)
map1: scala.collection.immutable.Map[String,Int] = Map(scala -> 1, java -> 2, python -> 3)
scala> map1("scala")
res25: Int = 1
# 可變map
scala> import scala.collection.mutable._
import scala.collection.mutable._
scala> val map1 = Map("scala"->1,"java"->2, "python"->3)
map1: scala.collection.mutable.Map[String,Int] = HashMap(python -> 3, java -> 2, scala -> 1)
scala> map1("scala") = 6
scala> map1.getOrElse("c#", -1)
res28: Int = -1
import scala.collection.mutable._
val map1 = new HashMap[String, Int]()
往里面增加數據:
map1("scala") = 1
map1 += (("java",2))
map1 += (("python",3),("C#",4))
map1.put("C++",6)
減少數據:
map1 -="java"
map1.remove("scala")
//生成一個list
val list0 = List(2,6,7,4,5,7,8,3,0,1)
//將list0中每個元素乘以2后生成一個新的集合
val list1 = list0.map(_ * 2)
//將list0中的偶數取出來生成一個新的集合
val list2 = list0.filter(_ % 2 ==0)
//將list0排序后生成一個新的集合
val list3 = list0.sorted
//反轉排序順序
val list4 = list3.reverse
//將list0中的元素4個一組,類型為Iterator[List[Int]]
val it = list0.grouped(4)
//將Iterator轉換成List
val list5 = it.toList
//將多個list壓扁成一個list
val list6 = list5.flatten
//先按空格切分,再壓平
val lines = List("hello java hello scala", "hello scala")
//方法一:
val words = lines.map(_.split(" "))
val flatWords = words.flatten
//方法二:
val res = lines.flatMap(_.split(" "))
//并行計算求和
val arr = Array(1,2,3,4,5,6,7,8,9)
val res1 = arr.sum
//并發
// val res2 = arr.par.sum
val res2 = (arr).sum
//按照特定的順序進行聚合
val res3 = arr.reduce(_+_)
//聚合多維數組
val list7 = List(List(1,2,3))
//求交集
val res4 = list1 intersect list2
//求差集
val res5 = list1 intersect(list2)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。