1 import scala.collection.mutable.ArrayBuffer
2 val a = new Array[String](10)
3 val s = Array("Hello","World")
4 val b = ArrayBuffer[Int]()
5
6 b+=1
7 b+=2
8 b+=3
9 println(b)
10
11 //immutable Map
12 val scores = Map("Alice" -> 10, "Bob" -> 3)
13 //can access element of Map by two ways
14 println(scores{"Alice"})
17 //scores{"good"} = 4
18
19 //mutable Map
20 val mscores = scala.collection.mutable.Map("Alice" -> 10, "Bob"-> 3)
21 println(mscores{"Bob"})
22 mscores{"good"} = 5
23 println(mscores{"good"})
24
25 for((k,v) <- mscores)
26 println(k+" "+ v)
27 println(mscores)
28
29 //exchange key and value
30 val mscores2 = for((k,v) <- mscores) yield (v,k)
31 println(mscores2)
32
33 //SortedMap, default Map is hash map, you can generate Tree Map
34 val sorted_scores = scala.collection.immutable.SortedMap("Alice" -> 10,"Fred" -> 7, "Bob" -> 3, "Cindy" -> 8)
35 println(sorted_scores)
2 val a = new Array[String](10)
3 val s = Array("Hello","World")
4 val b = ArrayBuffer[Int]()
5
6 b+=1
7 b+=2
8 b+=3
9 println(b)
10
11 //immutable Map
12 val scores = Map("Alice" -> 10, "Bob" -> 3)
13 //can access element of Map by two ways
14 println(scores{"Alice"})
15 println(scores("Alice"))
17 //scores{"good"} = 4
18
19 //mutable Map
20 val mscores = scala.collection.mutable.Map("Alice" -> 10, "Bob"-> 3)
21 println(mscores{"Bob"})
22 mscores{"good"} = 5
23 println(mscores{"good"})
24
25 for((k,v) <- mscores)
26 println(k+" "+ v)
27 println(mscores)
28
29 //exchange key and value
30 val mscores2 = for((k,v) <- mscores) yield (v,k)
31 println(mscores2)
32
33 //SortedMap, default Map is hash map, you can generate Tree Map
34 val sorted_scores = scala.collection.immutable.SortedMap("Alice" -> 10,"Fred" -> 7, "Bob" -> 3, "Cindy" -> 8)
35 println(sorted_scores)