1. Array("j.cole","big sean","kendrick","drake")
Array.mkstring 加("<", "," , ">")<j.cole,big sean,kendrick,drake>
2. 数组相关操作
val a = Array(1,2,3) 不可扩容
val b = scala.collection.mutable.ArrayBuffer[Int]()
b.insert(0,0)
b.remove(0)
b += 1
b += (1,2)
b ++= Array(3,4)
for (ele <- b) {
println(ele)
}
3.List
Nil其实就是一个空的集合
val l = List(1,2,3,4,5)
val b = scala.collection.mutable.ListBuffer[Int]()
list是由head和tail构成的
l.head = 1
l.tail = (2,3,4,5)
val l1 = 1 :: Nil --> List(1)
val l2 = 2 :: l1 --> List(2,1)
4.Map
val a = Map("Tmac" -> 33,"tracy" ->44)
println(a)
a += ("masiwei" -> 24,"knowknow" -> 26)
println(a)
for ((key,value) <- a){
println(key + ": " + value)
}
for (key <- a.keySet) {
println(key + ": " + a.getOrElse(key,9))
}
5.
None 是case object
Some 是case class
都继承了Option,none是 isEmpty==true
6.Tuple (元组)
val a = (1,2,3,4,5)
a._1 = 1
a._2 = 2
for(i <- 0 until(a.productAruity)){
println(a.productelement(i)
}
模式匹配
Java:对一个值进行条件判断,返回针对不同的条件针对不同的处理
变量 match{
case value1 => 代码
case value2 => 代码
case value3 => 代码
...
case_ => 代码
}
Example
val grades = Map("tracy" -> "A","masiwei" -> "B","psyp" -> "C")
def getGrade(name:String): Unit ={
val grade = grades.get(name)
grade match {
case **Some(grade)** => println(name + " your grade is " + grade)
case **None** => println("not exist")
}
}
getGrade("tracy")
getGrade("nono")
偏函数
partial function ,不包含match关键字,直接根据Input给出case的输出
def getGrade2:PartialFunction[String,String]={
case "A" => "you nailed it"
case "B" => "you just so so bro"
case "c" => "you need to work harder"
case _ => "you trash bro"
}
println(getGrade2("A"))
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。