Scala学习(一)
程序员文章站
2022-03-23 08:26:39
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 }
上一篇: 手机厂商去挖个时尚快消圈CMO吧
下一篇: 谁还不是个小公举