Scala学习笔记(一)

scala 版HelloWorrld

object HelloWorld{
   def main(args:Array[String]){
   println("Hello World!!!");
}
}

1.使用scalac HelloWorld.scala 编译下:

2.运行scala程序:scala HelloWorld

scala 无基本类型和包装类型之分
scala 方法: block内最后一行为返回值
没有返回值的方法写法:

scala> def sayHello(name:String){
| println("hello "+name)
 }
sayHello: (name: String)Unit

方法注意:

1.没有参数的方法可以不带圆括号访问

scala> def sayHello{
| println("hello world")
| }
sayHello: Unit

scala> sayHello
hello world

2.scala 没有静态方法,通过object 来实现

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")
}
}

带名参数

(赋值时顺序与定义时顺序不一致)

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 *)

//变长参数
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)
}

流程控制

if:

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 循环可以做条件过滤

for(i<-1 to 10 if i % 2==0){
println(i)
}

1 to 10 = 1.to(10)
1.until(10) 10没有了

集合

定长数组

val array_name = new ArrayT
val array_name = Array(“a”,”b”,”c”)
通过()访问,而不是[]
丰富的方法:
sum/max/min/
reverse

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

Map

不可变: 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)

Map的迭代:

for((k,v) <- map){ }
也可单独去key或value
for((k,_)) <- map)
for((_,v) <- map)
或者
map.keys / map.keySet
map.values

map遍历:

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

你可能感兴趣的:(Scala学习笔记(一))