scala 学习笔记(一)

基本数据结构

变量
  1. var
  2. val: 类似 java 的 final 变量,一旦初始化就不能再赋值了
scala> var msg = "hello"
msg: String = hello

scala> val msg = "world"
msg: String = world

scala> msg = "scala"
<console>:11: error: reassignment to val

为什么 scala 鼓励使用 val 而不是 var?

  1. val 是函数式风格,而 var 是指令式,val 的值是不变的,代码会比较容易阅读和重构;
  2. val 变量等效于计算它的表达式,可以用表达式代替变量名;
Array

scala 数组长度不可变,但元素值可变

scala> var arr = new Array[String](3)
arr: Array[String] = Array(null, null, null)
scala> arr(0) = "one" // 索引放在()中而不是[]
scala> arr(1) = "two"
scala> arr(2) = "three"
scala> arr
res8: Array[String] = Array(one, two, three)

scala> var arr = new Array[Int](3)
arr: Array[Int] = Array(0, 0, 0)

//更简洁的方法创造和初始化数组
scala> var arr = Array(0,1,2)
arr: Array[Int] = Array(0, 1, 2)

scala> var arr = Array(1, "2", 3)
arr: Array[Any] = Array(1, 2, 3)
// scala 数组元素类型不可变
scala> arr[0] = "1"
<console>:1: error: identifier expected but integer literal found.
List

与 Array 不同,List 是不可变对象

scala> var list = List(1, 2, 3)
list: List[Int] = List(1, 2, 3)

scala> var list_a = 1 :: 2 :: 3 :: Nil
list_a: List[Int] = List(1, 2, 3)
scala> var list_b = 4 :: 5 :: 6 :: Nil
list_b: List[Int] = List(4, 5, 6)

scala> var list_c = list_a ::: list_b
list_c: List[Int] = List(1, 2, 3, 4, 5, 6)
Tuple

元组也是不可变的,但可以包含不同类型的元素

scala> var tuple = (1, "2", 3)
tuple: (Int, String, Int) = (1,2,3)
//不能用()来访问元组的元素
scala> tuple(0)
<console>:12: error: (Int, String, Int) does not take parameters
//元组的 index 是从 1 开始的
scala> tuple._1
res37: Int = 1

scala> tuple._2
res38: String = 2

scala> tuple._3
res39: Int = 3
Set
// 默认为不可变集
scala> var set = Set(1, 2)
set: scala.collection.immutable.Set[Int] = Set(1, 2)

scala> set += 3
// 此时 set 是一个全新的集合
scala> set
res41: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

// 使用可变集合
scala> import scala.collection.mutable.Set

scala> var set = Set("a", "b")
set: scala.collection.mutable.Set[String] = Set(a, b)
// 可变集和将元素加入自身
scala> set += "c"
res43: scala.collection.mutable.Set[String] = Set(c, a, b)
Map
scala> var map = Map[Int, String]()
map: scala.collection.immutable.Map[Int,String] = Map()

scala> map += (1 -> "a")

scala> map += (2 -> "b")

scala> map
res46: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b)

scala> map(1)
res48: String = a
scala> map(2)
res49: String = b
// 简写,不可变映射是缺省的
scala> var map = Map(1 -> "a", 2 -> "b")
map: scala.collection.immutable.Map[Int,String] = Map(1 -> a, 2 -> b)

控制结构

if 表达式
scala> var a = "hello"
a: String = hello

scala> if(!a.isEmpty)
     | print("hello")
hello

scala> if(a.isEmpty)
     | print("a") else
     | print("b")
b
scala> if(a.isEmpty) println("a") else print("b")
b
while 循环
scala> var num = 1
num: Int = 1

scala> do {
     | num += 1
     | println("num is:" + num)
     | } while (num < 3)
num is:2
num is:3

scala> while (num != 6) {
     | num += 1
     | println("num is:" + num)
     | }
num is:4
num is:5
num is:6
for 表达式
scala> for(i <- 1 to 5)
     | println(i)
1
2
3
4
5

scala> for(i <- 1 until 5)
     | println(i)
1
2
3
4

scala> for( i <- 1 to 10 if i < 5)
     | println(i)
1
2
3
4

scala> for( i <- 1 to 10 if i > 5 && i < 8)
     | println(i)
6
7

scala> for( i <- 1 to 10
     | if i > 5; // 如果有多个 if 语句必须以分号隔开
     | if i < 8
     | ) println(i)
6
7

// foreach
scala> var a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> a.foreach(i => println(i))
1
2
3
异常处理
scala> try {
     | val result = 1 / 0
     | } catch {
     |   case ex: ArithmeticException => println("ArithmeticException")
     | } finally {
     |   println("ending!!!")
     | }
ArithmeticException
ending!!!
comments powered by Disqus