scala学习二:scala进阶
标签(空格分隔): scala
一,函数的求值策略
call by value
call by name(=>)
def test1(x:Int,y:Int):Int=x+x //> test1: (x: Int, y: Int)Int
def test2(x: => Int,y: => Int):Int=x+x //> test2: (x: => Int, y: => Int)Int
test1(3+4,8) //> res0: Int = 14
test2(3+4,8) //> res1: Int = 14
二,scala的函数参数
1,默认参数
def func1(name:String="tom")="hello "+name //> func1: (name: String)String
func1() //> res0: String = hello tom
func1("marray") //> res1: String = hello marray
2,带名参数
def func2(name:String="tom",age:Int=20)={
name+" age is "+age
} //> func2: (name: String, age: Int)String
func2() //> res2: String = tom age is 20
func2(age=25) //> res3: String = tom age is 25
3,变长参数(*)
def sum(args:Int*)={
var result=0
for(arg <- args)
result+=arg
result
} //> sum: (args: Int*)Int
sum(1,2,3) //> res4: Int = 6
三,scala的Lazy值
当val被申明为lazy时,它的初始化被推迟,直到我们首次使用它。
val words="hello world" //> words : String = hello world
lazy val words2="hello world" //> words2: => String
words2 //> res0: String = hello world
四,scala的异常处理
throw new Exception("some error happened")
//> java.lang.Exception: some error happened
//| at demo5$$anonfun$main$1.apply$mcV$sp(demo5.scala:10)
//| at org.scalaide.worksheet.runtime.library.WorksheetSupport$$anonfun$$exe
//| cute$1.apply$mcV$sp(WorksheetSupport.scala:76)
//| at org.scalaide.worksheet.runtime.library.WorksheetSupport$.redirected(W
//| orksheetSupport.scala:65)
//| at org.scalaide.worksheet.runtime.library.WorksheetSupport$.$execute(Wor
//| ksheetSupport.scala:75)
//| at demo5$.main(demo5.scala:1)
//| at demo5.main(demo5.scala)
try{
var words=scala.io.Source.fromFile("d:\\temp\\a.txt").mkString
}catch{
case ex:java.io.FileNotFoundException => {
println("========error block===================================")
}
}finally{
println("========finally block===================================")
}
//> ========error block===================================
//| ========finally block===================================
五,数组类型
定长数组(Array)
val a=new Array[Int](10)
val b=new Array[String](10)
val c=Array("tom","mike","mary")
变长数组(ArrayBuffer)
val d=ArrayBuffer[Int]()
d += 1
d += 2
d += (10,11,12)
//去掉最后2个值
d.trimEnd(2)
//convert to Array
d.toArray
数组的遍历
val c=Array("tom","mike","mary")
//使用for循环遍历
for(s <- c) println(s)
//对数组进行转换,新生成一个数组
val n=for{
s <- c
s1=s.toUpperCase
}yield(s1)
数组的常用操作:
val myArray=Array(1,10,2,3,5,4) //> myArray : Array[Int] = Array(1, 10, 2, 3, 5, 4)
//max
myArray.max //> res0: Int = 10
//min
myArray.min //> res1: Int = 1
//sum
myArray.sum //> res2: Int = 25
val myArray=ArrayBuffer(1,10,2,3,5,4)
//sort desc
myArray.sortWith(_>_)
//asc
myArray.sortWith(_<_)
多维数组:
val myArray=Array.ofDim[Int](3,4)
//Array[Array[Int]] = Array(Array(0, 0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0))
myArray(1)(2)=10
//Array[Array[Int]] = Array(Array(0, 0, 0, 0), Array(0, 0, 10, 0), Array(0, 0, 0, 0))
val triangle=new Array[Array[Int]](10)
for(i <- 0 until triangle.length){
triangle(i)=new Array[Int](i+1)
}
triangle
res1: Array[Array[Int]] = Array(Array(0), Array(0, 0), Array(0, 0, 0), Array(0, 0, 0, 0), Array(0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0, 0), Array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
scala数组和java集合的转换
import scala.collection.mutable.ArrayBuffer
import scala.collection.JavaConversions.bufferAsJavaList
object demo5 {
val buffer1 = ArrayBuffer("tom","mike","mary")
//> buffer1 : scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(tom, m
//| ike, mary)
val pb=new ProcessBuilder(buffer1) //> pb : ProcessBuilder = java.lang.ProcessBuilder@4ada629a
pb.command() //> res0: java.util.List[String] = [tom, mike, mary]
}
六,scala的Map和Tuple
import scala.collection.mutable.ArrayBuffer
import scala.collection.JavaConversions.bufferAsJavaList
object demo5 {
//不可变的map
val math=scala.collection.immutable.Map("alice" -> 80,"bob" -> 95,"mary" -> 70)
//> math : scala.collection.immutable.Map[String,Int] = Map(alice -> 80, bob ->
//| 95, mary -> 70)
//可变的map
val english=scala.collection.mutable.Map("alice" -> 80,"bob" -> 95,"mary" -> 70)
//> english : scala.collection.mutable.Map[String,Int] = Map(alice -> 80, bob -
//| > 95, mary -> 70)
val chinese=scala.collection.mutable.Map(("alice",80),("bob",95),("mary",70))
//> chinese : scala.collection.mutable.Map[String,Int] = Map(alice -> 80, bob -
//| > 95, mary -> 70)
}
//获取map的值
if(chinese.contains("alice")){
chinese("alice")
}else{
-1
}
//简写
chinese.getOrElse("bob",-1)
//更新map的值
chinese("bob") =100
//添加新的元素
chinese +="tom" ->56 //> res2: demo5.chinese.type = Map(alice -> 80, bob -> 100, tom -> 56, mary -> 7
//| 0)
//删除元素
chinese -="tom" //> res3: demo5.chinese.type = Map(alice -> 80, bob -> 100, mary -> 70)
迭代获取map的值:
for(s <- chinese) println(s)
tuple:不同类型值得集合
import scala.collection.mutable.ArrayBuffer
import scala.collection.JavaConversions.bufferAsJavaList
object demo5 {
val t1 =(1,2,"tom") //> t1 : (Int, Int, String) = (1,2,tom)
val t2=new Tuple2("mary",2) //> t2 : (String, Int) = (mary,2)
t2._1 //> res0: String = mary
t2._2 //> res1: Int = 2
t2.productIterator.foreach(println) //> mary
//| 2
}