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强大很多。

  1. 通配符匹配(Wildcard Pattern Matching )
  2. 常量匹配 (Constant Pattern Matching )
  3. 变量匹配(Variable Pattern Matching )
  4. 构造函数匹配(Constructor Pattern Matching )
  5. 集合类型匹配(Sequence Pattern Matching )
  6. 元祖类型匹配(Tuple Pattern Matching )
  7. 类型匹配(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

时间: 2024-08-29 05:45:19

spark快速开发之scala基础之2控制流程的相关文章

spark快速开发之scala基础之3类,对象,特征

类 scala的类定义非常灵活 class test4 class test2{} class test3(x:Int) 定义一个带构造函数的类 class Point (x : Int,y : Int){ def add() : Int = { x1 + y2 } } 通过this来重写构造函数 def this(X1 : Int){ this(X1,1) } def this(X2 : String){ this(0,1) } 除了重写构造函数,还可以当作当前对象的引用. def add(x

Android快速开发之appBase——(6).HttpReq和APICloudSDK

Android快速开发之appBase--(6).HttpReq和APICloudSDK HttpReq和APICloudSDK都是网络请求组件,都是基于xUtils的HttpUtils重新封装的.接下来讲一下使用方法. 1.HttpReq 看以看到有这么几个方法 GET:GET方式请求 POST:普通的POST表单提交 POST:将数据以流的形式传递 /** * POST请求,用InputStream的方式传递请求参数 * * @param api * 接口地址 * @param reques

Android快速开发之appBase——(1).appBase介绍

Android快速开发之appBase--(1).appBase介绍 一直想写博客,苦于自己的文笔实在不行,在CSDN潜水了好几年,中间差不多3年没有写过博客.原因有二:1.文笔差:2.没时间. 今年开始,时间充裕了,开始计划练练自己的文笔,也让自己成长起来,希望从中能够提升自己的能力.望大家多多支持和关注!! 导读:appBase是什么? appBase是一个Android app开发的基础集合,目的是任何应用都可以在这个基础之上开发app,省去了搭建框架的时间. appBase=xutils

Android快速开发之appBase——(4).详解com.snicesoft.Application和BaseActivity

Android快速开发之appBase--(4).详解com.snicesoft.Application和BaseActivity 在Android快速开发之appBase--(1).appBase介绍中使用过com.snicesoft.Application和BaseActivity,本篇则解开她们的面纱. 1. com.snicesoft.Application 1) 源码分析 package com.snicesoft; import java.util.ArrayList; import

[Java Web] 3\WEB开发之HTML基础程序试手

1.初试: 1 <html> 2 <body> 3 <h1>My First Heading</h1> 4 <p>My first paragraph.</p> 5 </body> 6 </html> 2.标题: HTML 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的.显然由大标题变为小标题.... 1 <html> 2 <body> 3

Android快速开发之appBase——(2).万能的Adapter

Android快速开发之appBase--(2).万能的Adapter android的Adapter是常用的一个组件,自定义的adapter基本上都是集成BaseAdapter,然后实现getView等一系列方法.时间长了,难免让人感觉到写的重复性代码过多,那么万能的Adapter讲解放你的双手. 对比 BaseAdapter package com.snicesoft.appbase.demo; import java.util.ArrayList; import java.util.Lis

Android快速开发之appBase——实战《购物车》

Android快速开发之appBase--实战<购物车> 最近将appBase实战于各种项目中,也发现了不少问题,并优化了很多功能.今天带给大家一个实战–<购物车>.购物车,在商城app中是必不可少的一部分,也是使用的比较多的,这里简单的做一个效果. 先来看看效果图 1.创建项目 第一种.引用appBase项目即可 第二种.将appBase的jar文件copy到libs下 我用的第二种,如上图所示. 2.代码生成 通过代码生成器生成Activity.Presenter.Adapte

crm快速开发之Entity

我们在后台代码里面操作Entity的时候,基本上是这样写的: /* 创建者:菜刀居士的博客 * 创建日期:2014年07月5号 */ namespace Net.CRM.Entity { using System; using Microsoft.Xrm.Sdk; /// <summary> /// 基本模式---Entity /// </summary> public class EntityDemo { public void Run(Entity entity) { if (

Android Studio快速开发之道

概述 现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的,今天就根据自己的经验来给大家介绍一下Android Studio快速开发之道. Postfix completion 介绍 Postfix completion 是IntelliJ IDEA很早就有的功能,该功能基于已经输入的表达式和你添加的后缀来实现另一个你想要的表达式.例如在布尔表达式之后加上后缀if就是if语句 . 常用后缀介绍 下面介绍一下个人工作中觉得比较常用的几个后缀