zoukankan      html  css  js  c++  java
  • Scala之for循环

    java中的for循环

    for (int j = 0; j <10 ; j++) {
        //循环体
    }
    
    for (String s: strings){
        //循环体
    }
    

    scala中for循环

    1.使用 1 to 5 表示循环的范围,从1到5(包含5)

    // 使用 1 to 5 表示循环的范围,从1到5(包含5)
    for (i <- 1 to 5) {
        print(s"i = ${i} 	")
    }
    

    在scala中1 to 5也可写为1.to(5)

    // 0.until(5) ==> 0 until 5
    // 0.to(5) ==> 0 to 5
    // 1.+(1) ==> 1 + 1
    for (i <- 1.to(5)) {
        print(s"i = ${i} 	")
    }
    

    scala中数字也是对象,可以调用方法

    scala是完全面向对象的语言,所以没有基本数据类型

    Scala 与 Java有着相同的数据类型,在Scala中数据类型都是对象,也就是说scala没有java中的原生类型

    2.使用 1 until 5 表示循环的范围,从1截止到5(不包含)

    //def until(end: Int): Range = Range(self, end)
    for (i <- 1 until 5) {
        print(s"i = ${i} 	")
    }
    
    
    // TODO Range(start,end) 范围对象,等同于until,不包含end
    // for循环的步长可以使用Range控制
    for (i <- Range(0, 5, 2)) {
        print(s"i = ${i} 	") //i = 0 	i = 2 	i = 4
    }
    

    3.嵌套循环

    for (i <- 1 to 3) {
        for (j <- 1 until 3) {
            println(s"${i} = ${j}")
        }
    }
    

    九层妖塔

    for (i <- Range(1, 18, 2)) {
        println(" " * ((18 - i) / 2) + "*" * i + " " * ((18 - i) / 2))
    }
    
    // TODO 没有关键字,所以范围后一定要加;来隔断逻辑
    for (i <- Range(1, 18, 2); j = (18 - i) / 2) {
        println(" " * j + "*" * i + " " * j)
    }
    
    // for循环中可以使用一行代码声明变量,也可以使用多行来声明变量,但是需要将小括号变成大括号
    // 表达式如果有多行代码,那么可以采用大括号声明
    // TODO 当for 推导式仅包含单一表达式时使用圆括号,当其包含多个表达式时使用大括号
    for {i <- Range(1, 18, 2)
         j = (18 - i) / 2} {
        println(" " * j + "*" * i + " " * j)
    }
    

    4.循环守卫

    // 循环守卫,即循环保护式(也称条件判断式,守卫)。保护式为true则进入循环体内部,为false则跳过,类似于continue
    for (i <- 1 to 5 if i % 2 == 0) {
        println(s"${i}")
    }
    

    5.默认情况下,for循环的返回值为()

    val unit: Unit = for (i <- 1 to 3) {
        "abc"
    }
    

    6.将遍历过程中处理的结果返回到一个新Vector集合中,使用yield关键字

    val res: immutable.IndexedSeq[Int] = for (i <- 1 to 10) yield i * 2
    println(res)
    

    7.中断循环break

    // java中采用break语法实现
    // scala中没有break关键字,但是可以采用对象的方式进行中断
    // try {} catch
    //Breaks.breakable()
    breakable {
        for (i <- 1 to 10) {
            if (i == 5) {
                //def break(): Nothing = { throw breakException }
                break
            }
            println(s"i = ${i}")
        }
    }
    
    
    breakable {
        for (i <- 1 to 20) {
            if (i >= 9) {
                println()
                break()
            }
            print(i + "	")
        }
    }
    
  • 相关阅读:
    949. Largest Time for Given Digits
    450. Delete Node in a BST
    983. Minimum Cost For Tickets
    16. 3Sum Closest java solutions
    73. Set Matrix Zeroes java solutions
    347. Top K Frequent Elements java solutions
    215. Kth Largest Element in an Array java solutions
    75. Sort Colors java solutions
    38. Count and Say java solutions
    371. Sum of Two Integers java solutions
  • 原文地址:https://www.cnblogs.com/chenxiaoge/p/13335448.html
Copyright © 2011-2022 走看看