Scala学习(一)
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 }