scala 循環(huán)
Scala循環(huán)中都沒(méi)有 break和continue悔橄,所以用if條件了(for)
for循環(huán)
scala循環(huán)有點(diǎn)怪異趾牧,例如:for循環(huán)
for(變量名 <- a to b)
eg: for(i <-1 to 10) print(i+" ") 輸出:1 2 3 4 5 6 7 8 9 10
- 表示變量 i 從1 到10停团,即:[1,10]
- 其中
<-
該符號(hào)中間不能有空格龟虎,寫(xiě)成 :< -
就會(huì)出錯(cuò)
加條件的for循環(huán)
for(i<-1 to 10 if i%2 ==0)
| print(i+",")
2,4,6,8,10,
- 上面i 范圍為:[1,10]踪旷,不過(guò)加上了一個(gè)條件:判斷i的奇偶性
- 如果出現(xiàn)兩個(gè)及以上循環(huán)for乍丈,則加上分號(hào)
- for循環(huán)中兩個(gè)及以上的 if 條件之間不用加分號(hào),如下所示
scala> for(i<-1 to 10 if i%2==0; j<-1 to 2 if i%3 ==0)
| print(i+",")
6,6,
scala> for(i<-1 to 10 if i%2==0; if i%3 ==0;j<-1 to 2 )
| print(i+",")
6,6,
scala> for(i<-1 to 10 if i%2==0 if i%3 ==0;j<-1 to 2 )
| print(i+",")
6,6,
while循環(huán)
scala> while(i<10)
| {print(i+",")
| i+=1
| }
1,2,3,4,5,6,7,8,9,
while (條件) 條件中不能加上if條件判斷
數(shù)組
數(shù)組有可變數(shù)組與不可變數(shù)組喉前,可變需要導(dǎo)入包没酣,在spark-shell中沒(méi)有安裝Scala,所以此處只有用不可變數(shù)組了
scala> var arr = Array(1,2,3,4,5)
arr: Array[Int] = Array(1, 2, 3, 4, 5)
scala> var arr2 = new Array[Int](5)
arr2: Array[Int] = Array(0, 0, 0, 0, 0)
scala> var arr = Array("hello"," world")
arr: Array[String] = Array(hello, " world")
scala> for(i<- 0 to 1)
| print(arr(i)+",")
hello, world,
- 將數(shù)組arr 初始化為兩個(gè)字符串
- 在調(diào)用數(shù)組各個(gè)變量時(shí)被饿,用arr(i)四康,而不是arr[i]搪搏,i 從0 開(kāi)始
Map映射
map映射就是鍵值對(duì)的映射狭握,和C++,Java中的Map差不多疯溺,都是不能重復(fù)
例如:
scala> val test1 = Map("a" -> 1,"b" -> 2,"a" -> 3)
test1: scala.collection.immutable.Map[String,Int] = Map(a -> 3, b -> 2)
scala> test1("b")
res4: Int = 2
scala> test1("a")
res3: Int = 3
- 形式為:Map(key1 -> value1,key2 -> value2)
- 變量名(key) 輸出對(duì)應(yīng)的value
- 如果key 有重復(fù)的论颅,過(guò)濾掉,取最后出現(xiàn)的一個(gè)value作為該key的value
Map 中的操作
- contains(key)囱嫩,如果有該key,返回true,否則 false
- getOrElse(key恃疯,num),如果有該key,返回對(duì)應(yīng)的value,如果沒(méi)有返回num
- drop(loc),返回從位置為loc之后的所有map對(duì)墨闲,從0開(kāi)始今妄,如下面所示
scala> val test2 = Map("a" -> 1,"b" -> 2,"c" -> 3)
test2: scala.collection.immutable.Map[String,Int] = Map(a -> 1, b -> 2, c -> 3)
scala> test2.contains("a")
res6: Boolean = true
scala> test2.drop(1)
res14: scala.collection.immutable.Map[String,Int] = Map(b -> 2, c -> 3)
scala> test2.drop(2)
res15: scala.collection.immutable.Map[String,Int] = Map(c -> 3)
scala> test2.drop(3)
res16: scala.collection.immutable.Map[String,Int] = Map()
Tuple元組
- 元組里面可以存放多種類(lèi)型的數(shù)據(jù)
- 原則遍歷是通過(guò):元組名._loc,(元組名鸳碧,點(diǎn)盾鳞,下劃線,元素位置)
- 元組中元素位置從1開(kāi)始
scala> val tuple1 = (1 ,3,"hello",5,"world",3,1)
tuple1: (Int, Int, String, Int, String, Int, Int) = (1,3,hello,5,world,3,1)
scala> print(tuple1._1)
1
scala> print(tuple1._3)
hello
Set集合
scala中的Set集合和C++瞻离、Java中的都差不多腾仅,都是包含沒(méi)有重復(fù)的元素
- 可以存放多種類(lèi)型,如String套利、Int混用
- 操作有:head,tail ,isEmpty推励,contains,drop 等
scala> val set1 = Set(1,7,5,3,8,3)
set1: scala.collection.immutable.Set[Int] = Set(5, 1, 7, 3, 8)
scala> var set3 = Set("a","b","c","a")
set3: scala.collection.immutable.Set[String] = Set(a, b, c)
// Int String
scala> var set2= Set(1,2,3,"a","b")
set2: scala.collection.immutable.Set[Any] = Set(1, a, 2, b, 3)