当前位置:网站首页>scala-for的基本应用
scala-for的基本应用
2022-06-22 15:24:00 【ZH519080】
scala-for的应用:
直接上代码。
def testfor01: Unit ={
val nieces = List("emily", "hananh", "mercedes", "porsche")
/*
关键字yield:for循环中的yield会把当前的元素记录下来,
保存到集合中,循环结束后将返回该集合。
scala中for循环是有返回值的,如果被循环的是Map,则返回的就是Map,
如果被循环的是List,则返回的是List,以此类推
scala遍历集合的方式较多:for循环、foreach循环、while循环、map映射、flatmap等
使用to或until进行for循环时,to与until区别:
to:包含上下限,是闭区间
until:不包含上下限,是开区间
*/
for (n <- nieces) yield println(n.capitalize)
/*
for (n <- nieces)这种for推导式中,失败的匹配则会被安静的忽略
*/
nieces.foreach(nie => println(nieces.mkString))
//2、for循环可作为计时器
println("------for as a timer------")
val a = Array("apple", "banana", "orange")
a.foreach(println)
for (i <- 0 until a.length if i != 1) println(s"$i is ${a(i)}")
//3、卫语句(for循环中if判断),此处to 与until的功能一样
//卫语句通常作为筛选集合,根据具体要求,可使用集合中的filter、take、drop等方法替代for循环
println("------ 在for循环中有if语句嵌套 ------")
for (x <- 0 to 5 if x != 3) println(x)
//在for循环中嵌套if语句,可以同时嵌套多个if语句
for (wei_x <- 0 to 5 if wei_x % 2 == 0 if wei_x == 2) println(wei_x)
System.out.println("------ 遍历Map集合 ------")
//遍历Map集合
val names = Map("fname" -> "Robert", "lname" -> "Goren")
for ((k, v) <- names) println(s"key: $k, value: $v")
}
def testfor02: Unit ={
val nieces = List("emily", "hananh", "mercedes", "porsche")
//for推导:通过for-yield的应用for循环是有返回值的。
val str01: List[String] = for (n <- nieces if !n.contains("emily")) yield n+" ... dd"
str01.foreach(println)
//for推导:在yield中有多行代码时,使用在yield中使用代码块
val str02 = for (n <- nieces if ! n.contains("emily")) yield {
val str = n+" ... dd"
str
}
str02.foreach(println)
}scala-for应用过程中注意for内的循环是开区间还是闭区间,若使用for-yield守卫,要注意yield的返回对象。
边栏推荐
- nio文件和文件夹操作例子
- Summary of JS methods for obtaining data types
- 变量
- 学习量子纠缠的可解释表示,该深度生成模型可直接应用于其他物理系统
- How to add a "security lock" to the mobile office of government and enterprises?
- 用递归法求Fibonacci数列第n项的值
- Prometheus监控之Consul监控 [consul-exporter]
- 代码扫描工具扫出的 Arrays.asList 使用BUG
- for.. of vs. for.. In statement
- shell学习
猜你喜欢
![[C language] deeply analyze the storage of integer and floating-point types in memory](/img/8b/12a4dc7a0c0e34e2add007592971dd.jpg)
[C language] deeply analyze the storage of integer and floating-point types in memory

jsp學習之(二)---------jsp脚本元素和指令

jsp学习之(一)---------jsp概述
![[wechat applet custom bottom tabbar]](/img/04/2ea4ab3fd8571499190a9b3c9990b2.png)
[wechat applet custom bottom tabbar]

SAP ABAP table control and example-07

IDEA安装总结

In case of default import failure

JS获取数据类型方法总结

SAP script tutorial: se71, se78, SCC1, vf03, so10-013

SAP ABAP dialog programming tutorial: module pool in -09
随机推荐
2.接口(计算器)
JS获取数据类型方法总结
nio服务多线程版本
Task scheduling design of collection system
【微信小程序封装底部弹出框二】
大话局部性原理
[C language] deeply analyze the relationship between pointer and array
NiO file and folder operation examples
[C language] use of library function qsort
Lecture 6 of slam Lecture 14 -- nonlinear optimization
Implementing factory mode using enumeration
招行23型号UKey在win7上无法识别
jsp学习之(二)---------jsp脚本元素和指令
[pop up box at the bottom of wechat applet package] I
Test for API
Summary of Changan chain usage skills
使用IDM让百度云加速的方法
Interface (optimization type annotation)
variable
让代码优雅起来(学会调试+代码风格)