文章目录
- 1. 数组
- 2. List
- 3. Set
- 4. Map
- 5. 元组
- 6. Option
- 7. 迭代器
学自 https://www.runoob.com/scala/scala-tutorial.html
1. 数组
- 使用
()
来取索引处的元素
// 数组var z1 : Array[String] = new Array[String](3)var z2 = new Array[String](3) // 两种方式定义z1(0) = "michael"; z1(1) = "scala"; z1(2) = "!"for(z <- z1)println(z)var z3 = Array("hello","michael","!!!")for(z <- z3)println(z)// 求和,求最大var nums = Array(1.9, 2, -1, 0.1)var sum = 0.0; var maxv = -100000.0for(i <- 0 to (nums.length-1)){sum += nums(i)if(nums(i) > maxv)maxv = nums(i)}println(printf("sum: %f, maxv: %f",sum, maxv))// sum: 3.000000, maxv: 2.000000()// 多维数组import Array._val mat = Array.ofDim[Int](3,3)for(i <- 0 to 2; j <- 0 to 2)mat(i)(j) = i*3+j;for(i <- 0 to 2; j <- 0 to 2)print(mat(i)(j) + " ") // 0 1 2 3 4 5 6 7 8println()// 合并数组var nums2 = Array(1,2,3)var nums3 = Array(4,5,6,7)var allnums = concat(nums2, nums3)for(x <- allnums)print(x + " ") // 1 2 3 4 5 6 7println()var nums4 = range(5, 8)for(x <- nums4)print(x + " ") // 5 6 7println()var nums5 = range(5, 18, 2) // 左闭右开,间距 2for(x <- nums5)print(x + " ") // 5 7 9 11 13 15 17println()
2. List
val site: List[String] = List("michael", "csdn","blog")val site1 = "michael" :: ("csdn" :: Nil)val empty: List[Nothing] = List()val empty1 = Nilval dim: List[List[Int]] = List(List(1, 0, 0),List(0, 1, 0),List(0, 0, 0, 1))val dim1 = (1::(0::(0::Nil)))::(0::(1::(0::Nil)))::(0::(0::(0::(1::Nil))))::Nilprintln(site.head) // michaelprintln(site.tail) // 除第一个元素之外的 List(csdn, blog)println(empty.isEmpty) // trueprintln(dim.isEmpty) // false// 列表连接// 方法1 ::: 运算var site0 = site ::: site1println(site0) // List(michael, csdn, blog, michael, csdn)// 方法2 List.:::() 方法, 注意连接的顺序site0 = site.:::(site1)println(site0) // List(michael, csdn, michael, csdn, blog)println(site) // List(michael, csdn, blog)// 方法3 使用 concat 方法site0 = List.concat(site, site1)println(site0) // List(michael, csdn, blog, michael, csdn)// List.fill() 填入重复数量的元素val site3 = List.fill(3)("michael") // 重复3次println(site3) // List(michael, michael, michael)// List.tabulate() 通过给定函数创建列表val squares = List.tabulate(6)(n => n*n+1)println(squares) // List(1, 2, 5, 10, 17, 26)// 二维val mul = List.tabulate(4,5)(_ * _)println(mul)// List(List(0, 0, 0, 0, 0), List(0, 1, 2, 3, 4), List(0, 2, 4, 6, 8), List(0, 3, 6, 9, 12))// List.reverse 反转println(squares.reverse) // List(26, 17, 10, 5, 2, 1)
3. Set
// set没有重复,默认是不可变集合, 改变后,生成新的setval set = Set(1,2,3)println(set.getClass.getName) // scala.collection.immutable.Set$Set3println(set.exists(_%2 == 0)) // trueprintln(set.drop(1)) // Set(2, 3)println(set) // Set(1, 2, 3)// 可变集合import scala.collection.mutableval mutableSet = mutable.Set(1,2,3)println(mutableSet.getClass.getName) // scala.collection.mutable.HashSetprintln(mutableSet.add(4)) // trueprintln(mutableSet.remove(1)) // truemutableSet += 5mutableSet -= 2println(mutableSet) // Set(5, 3, 4)// .head .tail .isEmpty 跟 List 的含义一样// 连接集合 ++ or Set.++() ,会去重val s1 = Set("michael","ming")val s2 = Set("michael", "scala")var s0 = s1 ++ s2println(s0) // Set(michael, ming, scala)s0 = s1.++(s2)println(s0) // Set(michael, ming, scala)// 最值 Set.min, Set.maxval num = Set(1,2,3,4,5,6,-1)println(num.min, num.max) // (-1, 6)// 交集 Set.& or Set.intersectval num1 = Set(1,2,3,11,12)println(num.&(num1)) // Set(1, 2, 3)println(num.intersect(num1)) // Set(1, 2, 3)
4. Map
// Map 默认不可变的 Map, 可变的要import scala.collection.mutable.Mapval colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF")println(colors("red")) // #FF0000println("all keys: " + colors.keys) // all keys: Set(red, azure)println("all values: " + colors.values) // all values: MapLike(#FF0000, #F0FFFF)println("isEmpty ? : " + colors.isEmpty) // isEmpty ? : falseval num3 = Map()println(num3.isEmpty) // true// 合并 ++运算 or Map.++()val colors1 = Map("red" -> "#FF0000","azure" -> "#F0FFFF","peru" -> "#CD853F")println(colors ++ colors1) // Map(red -> #FF0000, azure -> #F0FFFF, peru -> #CD853F)println(colors.++(colors1)) // Map(red -> #FF0000, azure -> #F0FFFF, peru -> #CD853F)// 如果有 key 一样,value不一样,以后者的为准,请自行测试// 输出 keys, valuescolors.keys.foreach{i => print("key: " + i); println(" value: " + colors(i))} // key: red value: #FF0000// key: azure value: #F0FFFF// 指定key是否存在println(colors.contains("red")) // trueprintln(colors.contains("blue")) // false
5. 元组
// 元组val t = (1, 3.14, "michael")println(t._1) // 1, 下标从1开始 ._idxt.productIterator.foreach{ // 遍历i => println("value: " + i)}// 元组转字符串println(t.toString()) // (1,3.14,michael)// 交换元素val t1 = (1,2)println(t1.swap) // (2,1) 长度只能为 2
6. Option
// Option 表示一个值是可选的val myMap = Map("k1" -> "v1")val v1 : Option[String] = myMap.get("k1")val v2 : Option[String] = myMap.get("k2")println(v1) // Some(v1)println(v2) // Nonedef show(x : Option[String]) = x match {case Some(s) => scase None => "?"}println(show(myMap.get("k1"))) // v1println(show(myMap.get("k2"))) // ?// getOrElse() 获取存在的元素或者使用默认值val a : Option[Int] = Some(5)val b : Option[Int] = Noneprintln(a.getOrElse(0)) // 5println(b.getOrElse(10)) // 不存在,取默认值10println(a.isEmpty) // falseprintln(b.isEmpty) // true
7. 迭代器
// Iterator 访问集合的方法// it.next() 返回下一个元素,并更新迭代器// it.hasNext 是否还有元素val it = Iterator("baidu", "alibaba", "tencent")while(it.hasNext){println(it.next())}val it1 = Iterator(1,2,3,-1)val it2 = Iterator(1,2,3,-1)val it3 = Iterator(1,2,3,-1)println(it1.min) // -1println(it2.max) // 3, 需要分开写,不能写 it1.maxprintln(it1.size) // 0, 迭代器走到头了,剩余长度0println(it3.length) // 4