十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
定长数组
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)
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
创新互联是一家专注于做网站、成都网站设计与策划设计,社旗网站建设哪家好?创新互联做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:社旗等地区。社旗做网站价格咨询:18980820575