@[toc]
集合
数组
package org.example
object ArrayApp extends App{
//println("hello")
val a = new Array[String](5)
a(0)="hello"
println(a(0))
val b = Array("hello","world")
val c = Array(1,2,3,4,5,67)
c.sum
c.max
c.mkString("/")
}
val d=scala.collection.mutable.ArrayBuffer[Int]()
d+=1
d+=2
d+=(2,33,4)
d++=Array(33,45,22)
println(d+"-------------------")
d.insert(0,999)
d.remove(1,2)
d.trimEnd(2)
println(d+"-------------------")
//转化成不可变的
d.toString()
for(i<-0 until d.length){
println(c(i))
}
hello
ArrayBuffer(1, 2, 2, 33, 4, 33, 45, 22)-------------------
ArrayBuffer(999, 2, 33, 4, 33)-------------------
1
2
3
4
5
List
list是不可变的,对list进行添加删除或者取值等操作均会返回一个新的list。
scala> Nil
res4: scala.collection.immutable.Nil.type = List()
scala> Nil
res4: scala.collection.immutable.Nil.type = List()
scala> val l= List(1,2,3,4,5,56)
l: List[Int] = List(1, 2, 3, 4, 5, 56)
scala> l.head
head headOption
scala> l.head
res5: Int = 1
scala> l.tail
tail tails
scala> l.tail
res6: List[Int] = List(2, 3, 4, 5, 56)
scala> l.tails
res7: Iterator[List[Int]] = non-empty iterator
scala>
val d=scala.collection.mutable.ArrayBuffer[Int]()
d+=1
d+=2
d+=(2,33,4)
d++=Array(33,45,22)
d++ =List(1,2,3,4,)
Set
set是一个非重复的集合,若有重复数据,则会自动去重。
scala> val set = Set(1,2,3,1,2,5)
set: scala.collection.immutable.Set[Int] = Set(1, 2, 3, 5)
Map
map是K-V键值对集合。
package org.example
object MapApp {
def main(args: Array[String]): Unit = {
val map = Map(
"1" -> "hello" ,
2 -> "world",
3 -> "!!!!!"
)
println(map.mkString(","))
println("-----------------------")
for(x<-map){
println(x._1+":"+x._2)
}
println("-----------------------")
var keys = map.keys
var keyIterator = keys.iterator
while(keyIterator.hasNext) {
val key = keyIterator.next()
println(key + "\t" + map.get(key).get)
}
}
}
1 -> hello,2 -> world,3 -> !!!!!
-----------------------
1:hello
2:world
3:!!!!!
-----------------------
1 hello
2 world
3 !!!!!
Optuon&Some&None
val map = Map(
"1" -> "hello" ,
2 -> "world",
3 -> "!!!!!"
)
println(map.get(2))
println(map.get(999))
Some(world)
None
option.scala
@SerialVersionUID(5066590221178148012L) // value computed by serialver for 2.11.2, annotation added in 2.11.4
case object None extends Option[Nothing] {
def isEmpty = true
def get = throw new NoSuchElementException("None.get")
}
@SerialVersionUID(1234815782226070388L) // value computed by serialver for 2.11.2, annotation added in 2.11.4
final case class Some[+A](x: A) extends Option[A] {
def isEmpty = false
def get = x
}
Tuple
与列表一样,与列表不同的是元组可以包含不同类型的元素。元组的值是通过将单个的值包含在圆括号中构成的。创建过程可加new关键词,也可不加。
package org.example
object TupleApp {
def main(args: Array[String]): Unit = {
var t=new Tuple3[Int,Int,String](1,99,"hello")
println(t.toString())
println("----------------")
var t2=(9999,"hello")
println(t2.toString())
println(t2.swap.toString())
}
}
(1,99,hello)
----------------
(9999,hello)
(hello,9999)
模式匹配
基本类型
Java : 对一个值进行条件判断例如switch
模式匹配类似于java的switch case。Scala的模式匹配不仅可以匹配值还可以匹配类型、从上到下顺序匹配,如果匹配到则不再往下匹配、都匹配不上时,会匹配到case _ ,相当于default、match 的最外面的”{ }”可以去掉看成一个语句。
def match_test(m:Any) = {
m match {
case 1 => println("nihao")
case m:Int => println("Int")
case _ => println("default")
}
}
package org.example
object MarchApp {
def main(args: Array[String]): Unit = {
def judeGrade(grade:String)={
grade match{
case "B" => println("Just so so")
case "A" => println("good")
case "S" => println("cool")
case _=> println("No.1")
}
}
judeGrade("S")
judeGrade("A")
judeGrade("SSS")
}
}
cool
good
No.1
Array
def greeting(array:Array[String]) = {
array match {
case Array("zs")=> println("hi,zs")
case Array(x,y)=> println(x+"and"+y)
case Array("zs",_*)=>println("zs and other")
case _=>println("everyone")
}
}
greeting(Array("zs"))
greeting(Array("zs","ls"))
hi,zs
zsandls
List
def greeting1(list:List[String]) = {
list match {
case "zs"::Nil=> println("hi,zs")
case x::y::Nil=> println(x+"and"+y)
case "zs"::tail =>println("zs and other")
case _=>println("everyone")
}
}
greeting1(List("zs"))
greeting1(List("zs","ls"))
}
类型匹配
def matchType(obj: Any) = {
obj match {
case x: Int => println("hi,int")
case y: String => println(y)
case m: Map[_, _] => println("map")
case _ => println("everyone")
}
}
matchType(Map(1 -> "yes"))
matchType(11)
matchType("hello")
}
异常处理
try{
val i=10/0
println(i)
}catch {
case e:ArithmeticException=>println(e.getMessage)
case e:Exception=>println(e.getMessage)
}finally {
}
/ by zero