1.scala面向行的語言,一句是一行的話,末尾的分號可有可無. 2.數據類型: Byte Short Int Long Float Double Char String Boolean,Unit,Nothing,Any,AnyRef 3.變數聲明:(聲明時必須賦初始值) ①變數: var str ...
1.scala面向行的語言,一句是一行的話,末尾的分號可有可無.
2.數據類型: Byte Short Int Long Float Double Char String Boolean,Unit,Nothing,Any,AnyRef
3.變數聲明:(聲明時必須賦初始值)
①變數: var str : String ="Peanut"
var str ="Peanut" (不指明數據類型也可以,會被推斷為String)
②常量: val num: Int =1
val num = 1 (不指明數據類型,被推斷為Int)
val a,b =10 (a,b都聲明為10)
4.集合(collection):
1 // 定義整型 List 2 val x = List(1,2,3,4) 3 4 // 定義 Set 5 val x = Set(1,3,5,7) 6 7 // 定義 Map 8 val x = Map("one" -> 1, "two" -> 2, "three" -> 3) 9 10 // 創建兩個不同類型元素的元組 11 val x = (10, "Runoob") 12 13 // 定義 Option 14 val x:Option[Int] = Some(5)
5.數組Array:
1 val arr1=Array(1,2,3) //val arr1=Array[Int](1,2,3) 2 val arr2=Array("a","b","c") //val arr2=Array[String]("a","b","c") 3 val arr3=ArrayBuffer(1,2,3) //val arr3=ArrayBuffer[Int](1,2,3) 4 arr3+=4 //ArrayBuffer長度可變,使用+=直接在末尾添加 5 val arr4=ArrayBuffer[String]("a","b","c") //val arr4=ArrayBuffer[String]("a","b","c") 6 arr4+=("d","f") 7 val arr5=arr1.toBuffer //Array轉ArrayBuffer 8 val arr6=arr3.toArray //ArrayBuffer轉Array 9 10 for(ele<-arr1){ print(ele+",")}//一維數組列印 11 12 val matrix=ofDim[String](2,3)//二維數組,第一個參數表示外層數組長度,第二個參數表示元素的數組長度 13 matrix(0)=Array("a1","b1","c1") 14 matrix(1)(0)="a2" 15 matrix(1)(1)="b2" 16 matrix(1)(2)="c2" 17 for(i<- 0 until(mytrix.length)){//二維數組列印 18 for(j<- 0 until(mytrix(i).length)){ 19 print(mytrix(i)(j)+",") 20 } 21 println() 22 }
6.鏈表List:
1 //列表拼接 2 List("a","b"):::List("c","d") //List(a,b,c,d) 3 List("a","b")++List("c","d") //List(a,b,c,d) 4 List("a","b")++:List("c","d") //List(a,b,c,d) 5 List("a","b")::List("c","d") //List(List(a, b), c, d) 6 List("a","b"):+List("c","d") //List(a, b, List(c, d)) 7 //列表長度 length 8 List("a","b").length //2 9 //**************************** 10 val fList=List("apple","banana","orange") 11 //最後一個元素 12 fList.last //orange 13 //除最後一個元素外所有元素 14 fList.init //List("apple","banana") 15 //反轉 16 fList.reverse //List("orange","banana","apple") 17 //取前n個元素 18 fList.take(2) //List("orange","banana") 19 //取第n個元素(從0開始) 20 fList.apply(2) //orange 21 fList(2) 22 //修改第n個元素 23 fList.updated(2,"orange2") //List("apple","banana","orange2") 24 //從第n分成兩個表 25 fList.splitAt(1) //(List("apple"),List("banana","orage")) 26 //除去前n個元素 27 fList.drop(2) //List("orange") 28 //*************************************** 29 val test = List('a','b','c','d','e') 30 //元素的索引 31 test.indices //Range(0, 1, 2, 3, 4) 32 //索引和元素組成的元組 33 test.indices zip test //Vector((0,a), (1,b), (2,c), (3,d), (4,e)) 34 //元素和索引組成的List 35 test.zipWithIndex //List((a,0), (b,1), (c,2), (d,3), (e,4)) 36 //mkString分割 37 test.mkString //abcde 38 test.mkString("#") //a#b#c#d#e 39 test.mkString("[","|","]") //[a|b|c|d|e] 40 //grouped分組 41 test.grouped(2).mkString(",") // List(a, b),List(c, d),List(e) 42 //************************ 43 //遍歷 44 data.foreach{x=>print(x+",")} //a,b,c,d,e, 45 data.map{x=>print(x+",")} //a,b,c,d,e,
7.映射Map:
1 //空哈希表,鍵為字元串,值為整型 2 var A:Map[Char,Int]=Map() 3 //Map鍵值對 4 val m1=Map("apple" -> 1,"orange" ->2,"strawberry"->5) 5 val m2=Map("apple" -> 3,"banana" ->4) 6 //++ Map合併,右邊覆蓋左邊的 7 val m3=m1++m2 //Map("apple" -> 3,"orange" ->2,"strawberry"->5,"banana" ->4) 8 val m4=m2++m1 //Map("apple" -> 1,"orange" ->2,"strawberry"->5,"banana" ->4) 9 10 m1.keys.foreach{i=> 11 println("key="+i) 12 println("value="+m1(i)) 13 }