1. 程式人生 > >scala語言入門(三)scala集合

scala語言入門(三)scala集合

一、定長陣列和變長陣列

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"

  println(a)
  println(a(0))

  val b = Array("hadoop", "hello", "spark", "hdfs", "mapredurce")

  println(b(0), b(1))

  val c = Array(2, 3, 4, 5, 6, 7, 8, 9)

  println(c.sum) //求和
  println(c.max) //最大值
  println(c.mkString) //轉為string
  println(c.mkString(",")) //以逗號隔開的string
  println(c.mkString("<", ",", ">")) // 開始和結束分別是< >

  //2.變長陣列
  val d = ArrayBuffer[Int]()

  //追加元素
  d += 1
  d += 2

  //追加多個元素
  d += (3, 4, 5)

  //直接追加一個數組
  d ++= Array(6, 7, 8)

  //在index 為0 的位置新增一個 0元素
  d.insert(0, 0)

  println(c.mkString(","))

  //移除
  d.remove(1)
  d.remove(0, 3)

  //從末尾移除
  d.trimEnd(2)

  d.toArray

  for (i <- 0 until d.length) {
    println(d(i))
  }

  for (ele <- d) {
    println(ele)
  }

  for (i <- (0 until d.length).reverse) {
    println(d(i))
  }
}

二、定長list,set ,變長list ,set 以及 模式匹配

object ListApp extends App {

  //list 有序的,可以重複的

  //定長list
  val l = List(11, 2, 3)
  println(l.head)
  println(l.tail)


  Nil // 等於 List(),空的不可變長的集合
  val l2 = 1 :: Nil
  val l3 = 2 :: l2
  val l4 = 1 :: 2 :: 3 :: 4 :: Nil

  //變長list
  val l5 = scala.collection.mutable.ListBuffer[Int]()

  l5 += 2
  l5 += (3, 4, 6)


  l5 ++= List(6, 7, 8, 9)

  l5 -= 2

  l5 -= 3
  l5 -= (1, 4)
  l5 --= List(5, 6, 7, 8)

  println(l5)

  l5.isEmpty
  l5.head
  l5.tail
  l5.tail.head

  def sum(nums: Int*): Int = {
    if (nums.length == 0) {
      0
    } else { //遞迴
      nums.head + sum(nums.tail: _*) //_* 將nums.tail轉換為 可變引數
    }
  }

  println(sum())
  println(sum(1, 2, 3, 4))


  //set 無序的,不重複的

  val s = Set(1, 2, 2, 1, 4, 3)
  println(s) //Set(1, 2, 4, 3)

  val s1 = scala.collection.mutable.Set[Int]()

  s1 += 1
  s1 += (11, 2, 3)
  s1 ++= Set(4, 5, 6)


  //模式匹配
  // match case 類似於 java 中的 switch case
  var names = Array("a", "b", "c");
  val name = names(Random.nextInt(names.length))

  name match {
    case "a" => println("季老師")
    case "b" => println("波老師")
    case _ => println("真不知道你們在說什麼。。。")
  }

  //case 不僅僅 可以匹配一個值,還可以加一個if條件進行過濾判斷
  //字串的模式匹配
  def judgeGrade(grade: String, name: String) = {
    grade match {
      case "A" => println("Excellent...")
      case "B" => println("Good ...")
      case "C" => println("just so so...")
      case _ if (name == "lisi") => println(name + ",you  are a good boy")
      case _ => println("you need work harder...")
    }
  }

  judgeGrade("f", "lisi")

  //陣列的模式匹配
  def greeting(array: Array[String]) = {
    array match {
      case Array("zhangsan") => println("hi:zhangsan")
      case Array(x, y) => println("hi :" + x + "," + y)
      case Array("zhangsan", _*) => println("hi zhangsan  and other friends ...")
      case _ => println("hi : everybody ...")
    }
  }

  greeting(Array("zhangsan"))

  //list的模式匹配
  def listMatch(list: List[String]): Unit = {
    list match {
      case "zhangsan" :: Nil => println("hi zhangsan")
      case x :: y :: Nil => println("hi:" + x + "," + y)
      case "zhangsan" :: tail => println("hi :zhangsan and other friends")
      case _ => println("hi :everybody....")
    }
  }

  listMatch(List("lisi"))


  //型別的模式匹配
  def  typeMatch(obj: Any): Unit = {
     obj match {
       case  x:Int =>println("Int")
       case  s:String =>println("String")
       case  m:Map[_,_]=>m.foreach(println)
       case  _ =>println("other type")
     }
  }

  typeMatch(Map("name"->"pk"))


  //sacla 的異常處理


  try {


  var i = 10 / 0
  println(i)
}catch{
  case  e:ArithmeticException=>println("除數不能為0")
  case  e:Exception =>println(e.getMessage())
}finally {
   //釋放資源,一定能執行
}





  //case  class 的模式匹配
  class Person
  case class CTO( name:String,floor:String ) extends  Person
  case class Employee( name:String,floor:String ) extends  Person
  case class Other( name:String) extends  Person

  def  caseClassMatch(person: Person): Unit ={
     person match {
       case  CTO(name,floor)=> println("cto name is "+name)
       case  Employee(name,floor) =>println("employee  name is "+name)
       case Other(name) =>println("other name is "+name)
     }
  }

  caseClassMatch(CTO("ctO","5"))

}