为了更好的驾驭spark,最近在学习scala语言特性,主要看《快学scala》,顺便把一些自己认为有用的代码记下来。
package examples import scala.util.control.Breaks._ object Example2 { def main(args: Array[String]) { //if else 语法 val s = if(1==1) 1 else 0 println(s) //代码块 val d = { val s=1+1 val g = s*2 g } println(d) //包含上界 for(i<-0 to 10) { print(i+",") } println() //不包含上界 for(i<-0 until 10) { print(i+",") } println() //普通循环不能用break.要用break必须要用breakable breakable{ for(i<-0 until 10) { if(1 == 1) { println("break!") break; } } } //多层嵌套的一种写法 for(i<-1 to 3;j<-1 to 3) print((i,j)) println() for(i<-1 to 3;j<-1 to 3 if i!=j) print((i,j)) println() for(i<-1 to 3;from=4-i;j<-from to 3) print((i,j)) println() //返回一个vector val numVector = for(i<-1 to 10) yield i%3 println(numVector) } }
1 4 0,1,2,3,4,5,6,7,8,9,10, 0,1,2,3,4,5,6,7,8,9, break! (1,1)(1,2)(1,3)(2,1)(2,2)(2,3)(3,1)(3,2)(3,3) (1,2)(1,3)(2,1)(2,3)(3,1)(3,2) (1,3)(2,2)(2,3)(3,1)(3,2)(3,3) Vector(1, 2, 0, 1, 2, 0, 1, 2, 0, 1)
本文链接:http://blog.csdn.net/lingerlanlan/article/details/43238055
本文作者:linger
时间: 2024-10-19 01:43:09