Scala学习记录(一)

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

 15 println(scores("Alice"))


 16 //can not insert new element
 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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值