简洁到不行,多一个分号都是不应该。
学习笔记:
不同于 java 的for循环:
/** * Created by ACER on 2017/7/22. */ object Test { def main(args: Array[String]) { //普通for循环 var a = 0 for (a <- 1 to 10){ print(a + ", ") } println() //不包含10 for(a <- 1 until 10){ print(a + ", ") } println() //不包含9,步长为2 for(a<- 1 until(9, 2)){ print(a + ", ") } println() //取笛卡尔积的for var b = 0 var c = 0 for(a <- 1 to 10;b <- 1 to 9){ c = c + 1 println("a: " + a) println("b: " + b) } //c输出为90 println("c:" + c) //遍历集合 var mlist = List(10,2,13) for (a <- mlist){ print(a + ", ") } println() //多条件过滤 for(a<-mlist if a!=2;if a>10){ print(a + ", ") } println() //使用yield关键字存储for循环的每次的结果 var d = 0; val numList = List(1,2,3,4,5,6,7,8,9,10); // for 循环,注意这里使用的是大括号 var retVal = for{ d <- numList if d != 3; if d < 8 }yield d // 输出返回值 var e = 0 for( e <- retVal){ println( "Value of d: " + e ); } } }
时间: 2024-10-06 06:57:04