小白教程
所有教程
关于
Search
108.162.216.92
108.162.216.92
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Scala for loop”的源代码
本页内容
上一节:
Scala_do_while_loop
下一节:
Scala_break_statement
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:for 循环}}[[Category:scala loop types|3]] = Scala for循环 = [[scala loop types|Scala 循环]] for 循环允许您编写一个执行指定次数的循环控制结构。 == 语法 == Scala 语言中 '''for''' 循环的语法: <pre> for( var x <- Range ){ statement(s); } </pre> 以上语法中,'''Range''' 可以是一个数字区间表示 '''i to j''' ,或者 '''i until j'''。左箭头 <- 用于为变量 x 赋值。 == 以下是一个使用了 '''i to j''' 语法(包含 j)的实例: <sample title="" desc="" lang="scala" hererun="1"> object Test { def main(args: Array[String]) { var a = 0; // for 循环 for( a <- 1 to 10){ println( "Value of a: " + a ); } } } </sample> 执行以上代码输出结果为: <pre> $ scalac Test.scala $ scala Test value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9 value of a: 10 </pre> 以下是一个使用了 '''i until j''' 语法(不包含 j)的实例: <sample title="" desc="" lang="scala" hererun="1"> object Test { def main(args: Array[String]) { var a = 0; // for 循环 for( a <- 1 until 10){ println( "Value of a: " + a ); } } } </sample> 执行以上代码输出结果为: <pre> $ scalac Test.scala $ scala Test value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9 </pre> 在 '''for 循环''' 中你可以使用分号 (;) 来设置多个区间,它将迭代给定区间所有的可能值。以下实例演示了两个区间的循环实例: <sample title="" desc="" lang="scala" hererun="1"> object Test { def main(args: Array[String]) { var a = 0; var b = 0; // for 循环 for( a <- 1 to 3; b <- 1 to 3){ println( "Value of a: " + a ); println( "Value of b: " + b ); } } } </sample> 执行以上代码输出结果为: <pre> $ scalac Test.scala $ scala Test Value of a: 1 Value of b: 1 Value of a: 1 Value of b: 2 Value of a: 1 Value of b: 3 Value of a: 2 Value of b: 1 Value of a: 2 Value of b: 2 Value of a: 2 Value of b: 3 Value of a: 3 Value of b: 1 Value of a: 3 Value of b: 2 Value of a: 3 Value of b: 3 </pre> == for 循环集合 == for 循环集合的语法如下: <pre> for( x <- List ){ statement(s); } </pre> 以上语法中, '''List''' 变量是一个集合,for 循环会迭代所有集合的元素。 == 以下实例将循环数字集合。我们使用 List() 来创建集合。再以后章节我们会详细介绍集合。 <sample title="" desc="" lang="scala" hererun="1"> object Test { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5,6); // for 循环 for( a <- numList ){ println( "Value of a: " + a ); } } } </sample> 执行以上代码输出结果为: <pre> $ scalac Test.scala $ scala Test value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6 </pre> == for 循环过滤 == Scala 可以使用一个或多个 '''if''' 语句来过滤一些元素。 以下是在 for 循环中使用过滤器的语法。 <pre> for( var x <- List if condition1; if condition2... ){ statement(s); } </pre> 你可以使用分号(;)来为表达式添加一个或多个的过滤条件。 == 以下是 for 循环中过滤的实例: <sample title="" desc="" lang="scala" hererun="1"> object Test { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5,6,7,8,9,10); // for 循环 for( a <- numList if a != 3; if a < 8 ){ println( "Value of a: " + a ); } } } </sample> 执行以上代码输出结果为: <pre> $ scalac Test.scala $ scala Test value of a: 1 value of a: 2 value of a: 4 value of a: 5 value of a: 6 value of a: 7 </pre> == for 使用 yield == 你可以将 for 循环的返回值作为一个变量存储。语法格式如下: <pre> var retVal = for{ var x <- List if condition1; if condition2... }yield x </pre> 注意大括号中用于保存变量和条件,retVal 是变量, 循环中的 yield 会把当前的元素记下来,保存在集合中,循环结束后将返回该集合。 == 以下实例演示了 for 循环中使用 yield: <sample title="" desc="" lang="scala" hererun="1"> object Test { def main(args: Array[String]) { var a = 0; val numList = List(1,2,3,4,5,6,7,8,9,10); // for 循环 var retVal = for{ a <- numList if a != 3; if a < 8 }yield a // 输出返回值 for( a <- retVal){ println( "Value of a: " + a ); } } } </sample> 执行以上代码输出结果为: <pre> $ scalac Test.scala $ scala Test value of a: 1 value of a: 2 value of a: 4 value of a: 5 value of a: 6 value of a: 7 </pre> [[scala loop types|Scala 循环]]
返回至“
Scala for loop
”。
上一节:
Scala_do_while_loop
下一节:
Scala_break_statement