scala语言入门(三)scala集合

举报
小米粒-biubiubiu 发表于 2020/12/03 00:16:04 2020/12/03
【摘要】 一、定长数组和变长数组 package com.dzx.listPack import scala.collection.mutable.ArrayBuffer object ArrayApp extends App { //1.定长数组 val a = new Array[String](5) a.length a(0) = "hello" printl...

一、定长数组和变长数组


  
  1. package com.dzx.listPack
  2. import scala.collection.mutable.ArrayBuffer
  3. object ArrayApp extends App {
  4. //1.定长数组
  5. val a = new Array[String](5)
  6. a.length
  7. a(0) = "hello"
  8. println(a)
  9. println(a(0))
  10. val b = Array("hadoop", "hello", "spark", "hdfs", "mapredurce")
  11. println(b(0), b(1))
  12. val c = Array(2, 3, 4, 5, 6, 7, 8, 9)
  13. println(c.sum) //求和
  14. println(c.max) //最大值
  15. println(c.mkString) //转为string
  16. println(c.mkString(",")) //以逗号隔开的string
  17. println(c.mkString("<", ",", ">")) // 开始和结束分别是< >
  18. //2.变长数组
  19. val d = ArrayBuffer[Int]()
  20. //追加元素
  21. d += 1
  22. d += 2
  23. //追加多个元素
  24. d += (3, 4, 5)
  25. //直接追加一个数组
  26. d ++= Array(6, 7, 8)
  27. //在index 为0 的位置添加一个 0元素
  28. d.insert(0, 0)
  29. println(c.mkString(","))
  30. //移除
  31. d.remove(1)
  32. d.remove(0, 3)
  33. //从末尾移除
  34. d.trimEnd(2)
  35. d.toArray
  36. for (i <- 0 until d.length) {
  37. println(d(i))
  38. }
  39. for (ele <- d) {
  40. println(ele)
  41. }
  42. for (i <- (0 until d.length).reverse) {
  43. println(d(i))
  44. }
  45. }

二、定长list,set ,变长list ,set 以及 模式匹配


  
  1. object ListApp extends App {
  2. //list 有序的,可以重复的
  3. //定长list
  4. val l = List(11, 2, 3)
  5. println(l.head)
  6. println(l.tail)
  7. Nil // 等于 List(),空的不可变长的集合
  8. val l2 = 1 :: Nil
  9. val l3 = 2 :: l2
  10. val l4 = 1 :: 2 :: 3 :: 4 :: Nil
  11. //变长list
  12. val l5 = scala.collection.mutable.ListBuffer[Int]()
  13. l5 += 2
  14. l5 += (3, 4, 6)
  15. l5 ++= List(6, 7, 8, 9)
  16. l5 -= 2
  17. l5 -= 3
  18. l5 -= (1, 4)
  19. l5 --= List(5, 6, 7, 8)
  20. println(l5)
  21. l5.isEmpty
  22. l5.head
  23. l5.tail
  24. l5.tail.head
  25. def sum(nums: Int*): Int = {
  26. if (nums.length == 0) {
  27. 0
  28. } else { //递归
  29. nums.head + sum(nums.tail: _*) //_* 将nums.tail转换为 可变参数
  30. }
  31. }
  32. println(sum())
  33. println(sum(1, 2, 3, 4))
  34. //set 无序的,不重复的
  35. val s = Set(1, 2, 2, 1, 4, 3)
  36. println(s) //Set(1, 2, 4, 3)
  37. val s1 = scala.collection.mutable.Set[Int]()
  38. s1 += 1
  39. s1 += (11, 2, 3)
  40. s1 ++= Set(4, 5, 6)
  41. //模式匹配
  42. // match case 类似于 java 中的 switch case
  43. var names = Array("a", "b", "c");
  44. val name = names(Random.nextInt(names.length))
  45. name match {
  46. case "a" => println("季老师")
  47. case "b" => println("波老师")
  48. case _ => println("真不知道你们在说什么。。。")
  49. }
  50. //case 不仅仅 可以匹配一个值,还可以加一个if条件进行过滤判断
  51. //字符串的模式匹配
  52. def judgeGrade(grade: String, name: String) = {
  53. grade match {
  54. case "A" => println("Excellent...")
  55. case "B" => println("Good ...")
  56. case "C" => println("just so so...")
  57. case _ if (name == "lisi") => println(name + ",you are a good boy")
  58. case _ => println("you need work harder...")
  59. }
  60. }
  61. judgeGrade("f", "lisi")
  62. //数组的模式匹配
  63. def greeting(array: Array[String]) = {
  64. array match {
  65. case Array("zhangsan") => println("hi:zhangsan")
  66. case Array(x, y) => println("hi :" + x + "," + y)
  67. case Array("zhangsan", _*) => println("hi zhangsan and other friends ...")
  68. case _ => println("hi : everybody ...")
  69. }
  70. }
  71. greeting(Array("zhangsan"))
  72. //list的模式匹配
  73. def listMatch(list: List[String]): Unit = {
  74. list match {
  75. case "zhangsan" :: Nil => println("hi zhangsan")
  76. case x :: y :: Nil => println("hi:" + x + "," + y)
  77. case "zhangsan" :: tail => println("hi :zhangsan and other friends")
  78. case _ => println("hi :everybody....")
  79. }
  80. }
  81. listMatch(List("lisi"))
  82. //类型的模式匹配
  83. def typeMatch(obj: Any): Unit = {
  84. obj match {
  85. case x:Int =>println("Int")
  86. case s:String =>println("String")
  87. case m:Map[_,_]=>m.foreach(println)
  88. case _ =>println("other type")
  89. }
  90. }
  91. typeMatch(Map("name"->"pk"))
  92. //sacla 的异常处理
  93. try {
  94. var i = 10 / 0
  95. println(i)
  96. }catch{
  97. case e:ArithmeticException=>println("除数不能为0")
  98. case e:Exception =>println(e.getMessage())
  99. }finally {
  100. //释放资源,一定能执行
  101. }
  102. //case class 的模式匹配
  103. class Person
  104. case class CTO( name:String,floor:String ) extends Person
  105. case class Employee( name:String,floor:String ) extends Person
  106. case class Other( name:String) extends Person
  107. def caseClassMatch(person: Person): Unit ={
  108. person match {
  109. case CTO(name,floor)=> println("cto name is "+name)
  110. case Employee(name,floor) =>println("employee name is "+name)
  111. case Other(name) =>println("other name is "+name)
  112. }
  113. }
  114. caseClassMatch(CTO("ctO","5"))
  115. }

 

文章来源: blog.csdn.net,作者:血煞风雨城2018,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/qq_31905135/article/details/85072111

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。