您的位置:首页 > 其它

云星数据---Scala实战系列(精品版)】:Scala入门教程046-Scala实战源码-Scala foreach操作

2017-11-29 09:55 996 查看

Scala foreach操作

package scala_learn.demo11_Collection

/**
* Created by liguohua on 201
4000
7/3/1.
*/
object O6_Foreach {
def main(args: Array[String]) {
test4
test3
test2
test1
}
def test4: Unit = {
val ls = List(2, 4, 3, 45, 109)
//使用通配符更简单
ls.foreach(print(_))
}

def test3: Unit = {
val ls = List(2, 4, 3, 45, 109)
//只有一个参数的匿名函数:x=>print(x+"\t")
ls.foreach(x => print(x + "\t"))
}

def test2: Unit = {
val ls = List(2, 4, 3, 45, 109)
//写成匿名函数:(x:Int)=>print(x+"\t")
ls.foreach((x: Int) => print(x + "\t"))
}

def test1: Unit = {
val ls = List(2, 4, 3, 45, 109)
//foreach是高阶函数,因为它函数中可以嵌套函数
ls.foreach(add(_))
}

def add(x: Int): Unit = {
print(x + "\t")
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐