数据类型(Array)
定长数组
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
数据类型(Map)
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)
//生成一个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)
网站栏目:scala笔记-基础数据类型日常操作
网页链接:
http://chengdu.cdxwcx.cn/article/iipdds.html