spark快速开发之scala基础之2控制流程
判断结构
大体与java相当。scala没有三元表达式。
val num = if(1>0) 1 else 0 //相当于匿名函数
println(num) var num2 = 0
if(1>0) num2 = 1 else num2 = 0 println(num2)
选择结构
match。与java的stiwch相当。但scala的match强大很多。
通配符匹配(Wildcard Pattern Matching )
常量匹配 (Constant Pattern Matching )
变量匹配(Variable Pattern Matching )
构造函数匹配(Constructor Pattern Matching )
集合类型匹配(Sequence Pattern Matching )
元祖类型匹配(Tuple Pattern Matching )
类型匹配(Typed Pattern Matching )
// constant patterns
case 0 => "zero"
case true => "true"
case "hello" => "you said 'hello'"
case Nil => "an empty List"
// sequence patterns
case List(0, _, _) => "a three-element list with 0 as the first element"
case List(1, _*) => "a list beginning with 1, having any number of elements"
case Vector(1, _*) => "a vector starting with 1, having any number of elements"
// tuples
case (a, b) => s"got $a and $b"
case (a, b, c) => s"got $a, $b, and $c"
// constructor patterns
case Person(first, "Alexander") => s"found an Alexander, first name = $first"
case Dog("Suka") => "found a dog named Suka"
// typed patterns
case s: String => s"you gave me this string: $s"
case i: Int => s"thanks for the int: $i"
case f: Float => s"thanks for the float: $f"
case a: Array[Int] => s"an array of int: ${a.mkString(",")}"
case as: Array[String] => s"an array of strings: ${as.mkString(",")}"
case d: Dog => s"dog: ${d.name}"
case list: List[_] => s"thanks for the List: $list"
case m: Map[_, _] => m.toString
// the default wildcard pattern
case _ => "Unknown"
循环结构
while
do while
与java相同。
for 可以多重循环,循环过滤。返回值。
val list = List("3423")
for(t <- list){
println(t)
}
for(i <- 1 to 10){//包含10
println(i)
}
for(i <- 1 until 10){//不包含10
println(i)
}
println("===================")
for(i <- 1 to 10;if i> 5){//添加过滤条件
println(i)
}
println("===================")
for(i <- 1 to 10;j <- 1 to 10){
println(i +" " + j)
}
println("===================")
for (i <- 1 to 5) yield i * 2
var result = for(t <- list) yield t //result = list
var result2 = for(t <- list) yield t + "10"
result.foreach(println)
异常控制
try{
}catch{
case ex : NullPointerException => ex.printStackTrace()
case _: Exception => ""
}
break continue
scala没有这两个关键字。但是scala提供了Breaks类来达到相同的效果。
def continue() {
for (i <- 1 to 10) {
Breaks.breakable({
if (i == 5) {
Breaks.break()
}
println(i)
})
}
println("break")
}
执行结果:
1
2
3
4
6
7
8
9
10
break
def break() {
Breaks.breakable({
for (i <- 1 to 10) {
if (i == 5) {
Breaks.break()
}
println(i)
}
})
println("break")
}
执行结果:
1
2
3
4
break
spark快速开发之scala基础之2控制流程的更多相关文章
- spark快速开发之scala基础之1 数据类型与容器
写在前面 面向java开发者.实际上,具有java基础学习scala是很容易.java也可以开发spark,并不比scala开发的spark程序慢.但学习scala可有助于更快更好的理解spark.比 ...
- spark快速开发之scala基础之5高阶函数,偏函数,闭包
高阶函数 高阶函数就是将函数作为参数或者返回值的函数. object function { def main(args: Array[String]): Unit = { println(test(f ...
- spark快速开发之scala基础之3类,对象,特征
类 scala的类定义非常灵活 class test4 class test2{} class test3(x:Int) 定义一个带构造函数的类 class Point (x : Int,y : In ...
- Android Studio快速开发之道
概述 现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的,今天就根据自己的经验来给大家介绍一下Android Studio快速开发之道. P ...
- iOS 开发之 GCD 基础
header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...
- ios开发之OC基础-类和对象
本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...
- ios开发之OC基础-oc小程序
本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...
- ios开发之OC基础-ios开发学习路线图
本系列的文章主要来自于个人在学习前锋教育-欧阳坚老师的iOS开发教程之OC语言教学视频所做的笔记,边看视频,边记录课程知识点.建议大家先过一遍视频,在看视频的过程中记录知识点关键字,把把握重点,然后再 ...
- Hybrid App开发之JavaScript基础
前言: 前面学习了html和css的基本使用,今天开始学习JavaScript的使用. 什么是JavaScript JavaScript是一种基于对象(Object)和事件驱动(Event Drive ...
随机推荐
- c++ map 官方样例
#include <iostream> #include <string> #include <iomanip> #include <map> temp ...
- Nop常用知识点
1.列表标题与内容均居中对齐,列中配置为: headerAttributes: { style: "text-align:center" }, attributes: { styl ...
- .Net MVC 获取Response和Request对象
通过 System.Web.HttpContext.Current 获取 public static string ConstractExportExcel(List<ERP_Contrac ...
- VMware vSphere 创建虚拟机步骤及三种磁盘规格
https://blog.csdn.net/hanzheng260561728/article/details/80471899 http://www.mycitrix.cn/esxi-disk-mo ...
- 2. oracle创建表空间、用户并设置默认表空间、授权
1.创建用户并设置默认表空间 create tablespace tablespacename datafile 'tablespacename.dbf' size 200m autoextend o ...
- asp.net Log4Net错误日志个人总结
1)创建Global.asax protected void Application_Start(object sender, EventArgs e) { log4net.Config.XmlCon ...
- python 3.4 error: Microsoft Visual C++ 10.0 is required(Unable to find vcvarsall.bat)
一些小技巧 我是在windows 64下安装的python3.4 Python 我在安装theano时报这个错,网上找了不少资料.自己摸索着解决了. 你先打开dos界面.我用set命令查看一下: 发现 ...
- WPF中TextBox文件拖放问题
在WPF中,当我们尝试向TextBox中拖放文件,从而获取其路径时,往往无法成功(拖放文字可以成功).造成这种原因关键是WPF的TextBox对拖放事件处理机制的不同,具体可参考这篇文章Textbox ...
- jsfl完成通知air
jsfl完成后生成一个文本A.txt, air开始jsfl执行后一直检测A.txt是否存在,存在就是完成了.那么就可以删除这个A.txt
- 17_react脚手架应用分析
|-- index.html // 启动页(主页) |-- build //构建目录,遵循发布系统规范 | |-- index.html //静态页面 | |-- static //资源文件发布到cd ...