(1)基本語法
變數
val var(可變變數);
資料型別
Byte,Char,Int,Short,Long,String(字元),Float,Double,Boolean(true,flase)。
列子:
val a = 3; or val a:Int = 3*//不可變,不能重新賦值
var b = 4.33; or var b:Double = 4.33 *//可變, 可賦值b = 3.9999;
字面量 :
5 + 6 ; == (5).+(6)
//scala是純面嚮物件語言,+為方法。3 max 5
//(基本資料型別福包裝類包含更多運算方法)
自增自減(不能使用a++,a–):
需使用a+=1,a-=1等等
建立Range:
1 to 5 *//Range(1,2,3,4,5) == 1.to(5)
1 until 5*//Range(1,2,3,4)
1 to 10 by 2*//以2為間隔生成Range。
0.5f to 5.9f by 0.3f
輸入輸出
輸入:(import scala.io.StdIn).readInt,readDouble,......
var i = readInt()*//輸入整型即可
輸出 print,println(輸出換行),printf(格式化輸出)
println(a), pirntf("output is %d,input is %.2f",a,b)
讀寫資料
寫(import java.io.PrintWriter)
val out = new PrintWrite("path/out.txt");
for( i <- 1 to 5) out.println(i)//寫入檔案out.txt
out.close
讀取(import scala.io.Source)
val readfile = Source.fromFile("out.txt")
val lines = readfile.getLines//返回結果lines為迭代器
for(line<- lines) println(line)
異常處理
例子:
import java.io.FileReader
import java.io.FileNotFoundException
import java.io.IOException
try{
val f = new FileReader("path/out.txt")
}
catch{
case ex: FileNotFoundException =>
print("file not found:)
case ex:IOException =>
print("io error")
} finally{
file.close()
}
控制語句(if,while,for)
val x=3
if(x>0){ ...} else if(x=3){...} else{...}*//if語句可以賦值給變數 val a = if(x=0) 1 else 0
where(x>0){ x-=1;printf(...)}
do{x+=1 println(i)}where(x<3)
for(i<- 1 to 5) println(i)
for(i<-1 to 5 if i%2==0) println(i)
fro(i<-1 to 5 if i%2!=0;j<-1 to 10 if j!=i) println(i*j)*//多個生成器+守衛(if)
val r = for(i<- 1 to 5 if i%2==0) yield {println(i);i} *//將for生成器生成的所有值收入集合r( Vector(2,4))
(2) 資料結構(容器collection)
*//import scala.collection,scala.collection.mutable(可變),scala.collection.immutable(不可變)預設
列表(List)
val strList = List("me","you","our")* //不可變列表 方法:head,tail
val otherList = "they"::strList
val List1 = 1::2::3::Nil == List(1,2,3)
集合(Set) 不重複元素
var myset = Set("hadop","spark") *//var可變,庫為不可變庫
myset+="Scala" *//新增元素
*//匯入可變包
import scala.collection.mutable.Set
val mySet = Set("a","b")*//val不可變,但Set可變
mySet+="c"
對映(map)
val mymap = Map("a"->1,"b"->2)*//鍵值對,鍵是唯一的,值不唯一
print(mymap("a"))*//索引
val c = if (mymap.contain("a")) mymap("a") else 0 *//contain判斷鍵是否存在
*//可變map (import scala.collection.mutable.Map)
mymap("a") = 3* //更新
mymap("c")= 4 *//新增新元素
mymap+=("d"->5,"e"->6) *//add
for((key,value)<-mymap) println(key,value) or for(key<-mymap.keys) print(key) or mymap.values(生成迭代器)
迭代器(Iteration)
val iter = Iteration(1,2,3,4)
while(iter.hasNext) { println(iter.next) } or for(elem<-iter) { printn(iter.next)}
方法:grouped,sliding返回迭代器
val myList = List(1,2,3,4,5)
val a = mylist grouped 3 //按照3分為兩組,返回迭代器,a.next=List(1,2,3) a.next = List(4,5)
val b = mylist sliding 3 //3個數滑動 b.next = List(1,2,3) .next = (2,3,4)....
陣列(Array)*//(變長陣列import scala.collection.mutable.ArrayBuffer)
val myarray = new Array[Int](3) or val myarray = new Array(2,3,4)
myarray(0) = 1 myarray(1)=2 myarray(2) = 3
for(elem<-0 to 2) { println(myarray(elem)) }
多維陣列 :val myarray = Array.ofDim[Int](3,4)
可變陣列 :val myarray = ArrayBuffer(10,20,30) myarray+=40
*//方法 insert ,remove
元組(tuple)
//資料型別可以不同
val mytuple = Tuple("a",1,3) println(mytuple._1) println(mytuple._2)
//索引