当前位置:网站首页>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的返回对象。
边栏推荐
- redis.clients.jedis.exceptions.JedisDataException ERR invalid password.
- Test for API
- SAP ABAP table control and example-07
- uniapp微信小程序获取页面二维码(带有参数)
- Jsp Learning (2) - - jsp script Elements and instructions
- Test for API
- jsp学习之(三)--------- jsp隐式对象
- Reddit对LaMDA模型的探讨:并非无状态,采用双重过程,相比它编辑维基百科的方式,有没有感情并不重要
- 超出文本部分用省略号表示
- Prometheus监控之Consul监控 [consul-exporter]
猜你喜欢

Lecture 6 of slam Lecture 14 -- nonlinear optimization

Safari兼容性问题总结

Summary of safari compatibility issues

Machine learning notes - Hagrid - Introduction to gesture recognition image data set

北京恢复堂食半月记:如何重燃门店经营烟火气

VHEDT业务发展框架

SAP ABAP data dictionary tutorial se11: tables, locked objects, views, and structures-03

全球首款AR隐形眼镜,元宇宙入口这次真的打开了?

'不敢去怀疑代码,又不得不怀疑代码'记一次网络请求超时分析

让代码优雅起来(学会调试+代码风格)
随机推荐
面对默认导入失败的情况
安全信得过!天翼云数据安全管理平台通过评测
Make the code elegant (learn debugging + code style)
ALV report in SAP tutorial - ABAP list viewer -012
什么是RESTful,REST api设计时应该遵守什么样的规则?
scala-for推导:能够在for表达式中的最初部分定义值,并在(外面)后面的表达式中使用该值
【C语言】深度剖析整型和浮点型在内存中的存储
[pop up box 2 at the bottom of wechat applet package]
3. Classe abstraite (forme)
招行23型号UKey在win7上无法识别
Oracle database and table
Prometheus监控之Consul监控 [consul-exporter]
3. abstract class (shape)
北京恢复堂食半月记:如何重燃门店经营烟火气
Iterators and generators
面试题之JS判断数据类型的方法
4. string (reverse order and case conversion)
如何为政企移动办公加上一道“安全锁”?
Test for API
4.字符串(倒序且大小写转换)