顿搜
飞过闲红千叶,夕岸在哪
类目归类
for(i <- 1 to 10) println(i)1 to 10 是一个 Range的聚类,其值为 Range(1,2,3,4,5,6,7,8,9,10),是一个有序的集合
for(i <- 1 until 10) println(i)值打印 1-9,不打印 10
val str = "hello scala"
for (c <- str) println(c)
for (i <- 0 until str.length) println(str.charAt(i))val arr= Array(3,24,36,42,50)
for (num <- arr) println(num)
for (i <- 0 until arr.length) println(arr(i))for(i <- arr) yeild i* 10
for(i <- arr if i % 2 == 0) yeild i * 10yeild 在 for 循环中相当于生成一个新的集合
上面代码等同于
arr.map(_ * 10)
arr.map(x => x * 10)
arr.filter(x => x % 2 == 0).map(_ * 10)for(i <- 1 to 3; j <- 1 to 3 if i != j){
print(i + j + " ");
}该代码相当于
for(i <- 1 to 3){
for( j <- 1 to 3){
if(if i != j){
print(i + j + " ");
}
}
}