object HelloWorld{
def main(args:Array[String]){
println("Hello World!!!");
}
}
1.使用scalac HelloWorld.scala 編譯下:java
2.運行scala程序:scala HelloWorld數組
scala 無基本類型和包裝類型之分
scala 方法: block內最後一行爲返回值
沒有返回值的方法寫法:markdown
scala> def sayHello(name:String){
| println("hello "+name)
}
sayHello: (name: String)Unit
1.沒有參數的方法能夠不帶圓括號訪問app
scala> def sayHello{
| println("hello world")
| }
sayHello: Unit
scala> sayHello
hello world
2.scala 沒有靜態方法,經過object 來實現less
class TestStaticObject { } object TestStaticFun{ def add(x:Int,y:Int)=x+y } object TestStaticObject{ def main(args: Array[String]): Unit = { val res = TestStaticFun.add(2,3) println(res) } }
def saySth(name:String="jack")
object TestFuncParam {
//參數默認值就是在參數定義時加=號指定一個default 值
def loadConf(conf:String="true") {
println(conf)
}
def main(args: Array[String]): Unit = {
loadConf()
// loadConf("ok")
}
}
(賦值時順序與定義時順序不一致)ide
object TestFuncParam {
//參數默認值就是在參數定義時加=號指定一個default 值
def loadConf(conf:String="true") {
println(conf)
}
def sayInfo(name:String,age:Int){
println(name+":"+age)
}
def main(args: Array[String]): Unit = {
loadConf()
// loadConf("ok")
sayInfo("tom",18)
//帶名參數
sayInfo(age=18,name="zhanggao")
}
}
(x :Int *)ui
//變長參數
def add(x:Int *)={
for(i<-x){
println(i)
}
x.sum //累加
}
def main(args: Array[String]): Unit = {
loadConf()
// loadConf("ok")
sayInfo("tom",18)
//帶名參數
sayInfo(age=18,name="zhanggao")
val res = add(4,5,6,7,8)
println(res)
}
val x = 11;
if(x>10) println("x is bigger than 10") else println("x is less than 10")
var result = 0
val a = 10
val b=20 //在java中的寫法 // if(a>b){ // result = 1 // }else{ // result = -1 // } //scala中的簡潔寫法
result = if(a>b) 1 else -1
for:
for(i<-1 to 10){
println(i)
}
//for 循環能夠作條件過濾es5
for(i<-1 to 10 if i % 2==0){
println(i)
}
1 to 10 = 1.to(10)
1.until(10) 10沒有了spa
val array_name = new ArrayT
val array_name = Array(「a」,」b」,」c」)
經過()訪問,而不是[]
豐富的方法:
sum/max/min/
reversescala
scala> val arr = new Array[String](3)
arr: Array[String] = Array(null, null, null)
scala> val arr1=Array(1,2,3);
arr1: Array[Int] = Array(1, 2, 3)
import scala.collection.mutable.ArrayBuffer
val buff = new ArrayBuffer[T]()
豐富的方法:
+=/++=/-=/–=/
insert/remove/toArray/trimEnd()
sum/max/min/
reverse/
scala> val arr3 = new scala.collection.mutable.ArrayBuffer[String]()
arr3: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer()
scala> arr3 +="tom"
res9: arr3.type = ArrayBuffer(tom)
scala> arr3 ++= Array("lucy","rose","ombama")
res10: arr3.type = ArrayBuffer(tom, lucy, rose, ombama)
scala> arr3 -= "tom"
res11: arr3.type = ArrayBuffer(lucy, rose, ombama)
scala> arr3.insert(2,"jack")
scala> arr3
res13: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(lucy, rose, jack, ombama)
scala> arr3.remove(2)
res14: String = jack
scala> arr3
res15: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(lucy, rose, ombama)
scala> arr3.trimEnd(2)
scala> arr3
res17: scala.collection.mutable.ArrayBuffer[String] = ArrayBuffer(lucy)
scala> val arr4 = new scala.collection.mutable.ArrayBuffer[Int]()
arr4: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer()
scala> arr4 ++= Array(2,34,5,6)
res18: arr4.type = ArrayBuffer(2, 34, 5, 6)
scala> arr4.sum
res19: Int = 47
scala> arr4.max
res20: Int = 34
scala> arr4.min
res21: Int = 2
//toArray並非把它本身變成定長數組,而是產生新的數組
scala> arr4.toArray
res22: Array[Int] = Array(2, 34, 5, 6)
scala> arr4
res23: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(2, 34, 5, 6)
定長數組也有 max、min、sum、reverse方法
scala> val arr = new Array[String](6)
arr: Array[String] = Array(null, null, null, null, null, null)
scala> for(i <- 0 until arr.length) arr(i)= i+""
scala> arr
res25: Array[String] = Array(0, 1, 2, 3, 4, 5)
scala> for(i <- 0 until arr.length) println(arr(i))
0
1
2
3
4
5
推薦寫法:
scala> for(i <- arr) println(i)
0
1
2
3
4
5
不可變: val age = Map(「jack」 -> 18,」lucy」 -> 20)
可變:
val info = scala.collection.mutable.Map(「a」 -> 1)
指定泛型:
val info = scala.collection.mutable.Map[String,Int]()
scala> val m = Map("jack"->18,"lucy"->20)
m: scala.collection.immutable.Map[String,Int] = Map(jack -> 18, lucy -> 20)
scala> m("jack")
res28: Int = 18
scala> m.get("jack")
res29: Option[Int] = Some(18)
scala> m.get("jack2")
res30: Option[Int] = None
scala> m("jack2")
java.util.NoSuchElementException: key not found: jack2
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:59)
at scala.collection.MapLike$class.apply(MapLike.scala:141)
at scala.collection.AbstractMap.apply(Map.scala:59)
... 33 elided
scala> m.getOrElse("jack",22)
res32: Int = 18
scala> m.getOrElse("jack2",22)
res33: Int = 22
scala> m.keys
res34: Iterable[String] = Set(jack, lucy)
scala> m.values
res35: Iterable[Int] = MapLike(18, 20)
可變map:
scala> val m2 = scala.collection.mutable.Map("tom"->16)
m2: scala.collection.mutable.Map[String,Int] = Map(tom -> 16)
//實際上是產生了一個新的Map對象
scala> m2 + ("jim"-> 20)
res36: scala.collection.mutable.Map[String,Int] = Map(jim -> 20, tom -> 16)
scala> m2
res37: scala.collection.mutable.Map[String,Int] = Map(tom -> 16)
定長Map:
scala> m + ("jim"->23)
res38: scala.collection.immutable.Map[String,Int] = Map(jack -> 18, lucy -> 20, jim -> 23)
scala> val m_new = m+("jim"->34)
m_new: scala.collection.immutable.Map[String,Int] = Map(jack -> 18, lucy -> 20, jim -> 34)
若是想在原來的m2(可變Map)基礎上加入一個k,v.
scala> m2+=("jim"->3232)
res39: m2.type = Map(jim -> 3232, tom -> 16)
scala> m2
res40: scala.collection.mutable.Map[String,Int] = Map(jim -> 3232, tom -> 16)
for((k,v) <- map){ }
也可單獨去key或value
for((k,_)) <- map)
for((_,v) <- map)
或者
map.keys / map.keySet
map.values
scala> for((k,v) <- m){
| println("key is "+k+",value is "+v)
| }
key is jack,value is 18
key is lucy,value is 20
只想遍歷value,如何作到:
scala> for((_,v)<- m){
| println("value is "+v)
| }
value is 18
value is 20
//只遍歷key
scala> for((k,_)<- m){
| println("key is "+k)
| }
key is jack
key is lucy
scala> m.keys
res46: Iterable[String] = Set(jack, lucy)
scala> m.keySet
res47: scala.collection.immutable.Set[String] = Set(jack, lucy)
scala> m.values
res48: Iterable[Int] = MapLike(18, 20)
val tuple = (1,2,3,4) //就是()裏包含一系列的值
經過 ._ 取值,下標從1開始,例如 t._1
迭代
for(elem <- t.productIterator) { }
scala> val tupe = (1,2,3,4)
tupe: (Int, Int, Int, Int) = (1,2,3,4)
//獲取元組值
scala> tupe._3
res49: Int = 3
scala> tupe.productIterator
res50: Iterator[Any] = non-empty iterator
scala> for(i <- tupe.productIterator) println(i)
1
2
3
4