scala第二课

if 判断
def min(x:Int,y:Int)=if(x>y) y else x
min(5,6)
while
var a=3;while(a!=0){println(a):a-=1}
for
发生器 函数体
for(i<- 1 to 5) println(i)
val s="hello";for(i<- 0 until s.length) println(s(i))
for(i<-1 to 6) if(i%2==0) println(i)
for(i<-1 to 6;if(i%2==0);if(i!=4))println(i)
for(i <-1 to 3) for(j< -1 to 3) println(i*j)
val num = for(i<- 1 to 9) yield i
for(i<- 1 to 2 ;j< -"hello") yield (i+j).toChar
scala匹配 --switch
val funame:"aaa":val fu=
    funame match{
    case "ap"=>"apple"
    case "ba"=>"banner"
    case _=>"other"
    
    }
funame:String =aaa
scala 异常
scala可变数组/定长数组
val numw= new Array[Int](10)
numw(3)
val a = new ArrayBuffer[Int]()
val b
scala 映射
val a =Map("a"->1,"b"->2)不可变映射
val c=scala.collection.mutable.Map("aa"->1,"bb"->2)可变映射
println(a("a"))
println(a.contains("gg"))
val is = if (a.contains("gg")) a("gg") else 0等同于 a.getOrElse("gg","not found"))
c("apple")=10
c +=("ee"->12,"ss"->15)
c -="ee"//删除
for((k,v)<-c)print(k+":"+v+"\t")
val keys =c.keySet
for(key<-keys) println(key)
val vas = v.values
for(vs<vas)println(vs)
val news =for((k,v)<-a) yield(v,k)//key value反转
for((k,v)<-news)print(k+":"+v+"\t")
for((i,j)<-Z) yield(j,i)
val e =Z.keySet
val f = Z.values
scala 元组
val t =(1,"String",7.23)//定义
println(t._1)//访问,索引从1开始
println(t _1)
匹配
val (first,serc,th) = t
组元的拉链操作
val cleft = Array("a","b","c","d")
val cright = Array("1","2","3","4")
val cto = cleft.zip(cright)
for((k,v)<-cto )print(k+":"+v+"\t")









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值