关闭

Scala(三)之“控制结构”

标签: scala遍历
136人阅读 评论(0) 收藏 举报
分类:
  1. for和foreach循环
    遍历一个集合中的所有元素,对集合中的每个元素进行操作,或者利用现有的集合创建一个新集合。
scala> val a = Array("apple", "banana", "orange")
a: Array[String] = Array(apple, banana, orange)

scala> for(e <- a) println(e)
apple
banana
orange

使用for/yield组合可以从输入的集合中生成一个新的集合。

scala> val newArray = for(e <- a) yield e.toUpperCase
newArray: Array[String] = Array(APPLE, BANANA, ORANGE)

for循环计数器

scala> for(i <- 0 until 10){
     | print(i)
     | }
0123456789

生成器与守卫语句

scala> for(i <- 1 to 3)println(i)
1
2
3

scala> 1 to 3
res47: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3)

scala> for(i <- 1 to 3) println(i)
1
2
3

scala> 1 to 3
res49: scala.collection.immutable.Range.Inclusive = Range(1, 2, 3)

scala> for(i <- 1 to 10 if i < 4) println(i)
1
2
3

遍历Map集合

scala> val names = Map("fname" -> "Robert",
     | "lname" -> "Goren")
names: scala.collection.immutable.Map[String,String] = Map(fname -> Robert, lname -> Goren)

scala> for((k, v) <- names) println(s"key: $k, value: $v")
key: fname, value: Robert
key: lname, value: Goren

for循环中使用多个计数器

scala> for(i <- 1 to 2; j <- 1 to 2) println(s"i = $i, j = $j")
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
// 对于对个for循环推荐使用大括号的风格
scala> for{
     | i <- 1 to 2
     | j <- 1 to 2
     | }println(s"i = $i, j = $j")
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2

for循环为多维数组赋值

scala> val array = Array.ofDim[Int](2, 2)
array: Array[Array[Int]] = Array(Array(0, 0), Array(0, 0))

scala> for{
     | i <- 0 to 1
     | j <- 0 to 1
     | }println(s"($i)($j) = ${array(i)(j)}")
(0)(0) = 0
(0)(1) = 0
(1)(0) = 0
(1)(1) = 0

for循环中嵌入if语句,if语句又叫做过滤器

scala> for{
     | i <- 1 to 10
     | if i % 2 == 0
     | }println(i)
2
4
6
8
10

scala> for{
     | i <- 1 to 10
     | if i > 3
     | if i < 6
     | if i % 2 == 0
     | }println(i)
4

创建for表达式(for/yield组合)
在一个已有的集合上,使用带有yield语句的for循环和算法,生成一个新的集合。
除了极个别的情况,for推导不会改变集合的返回类型

scala> val name = Array("chris", "ed", "maurice")
name: Array[String] = Array(chris, ed, maurice)

scala> val capName = for(e <- name) yield e.capitalize
capName: Array[String] = Array(Chris, Ed, Maurice)

Scala中没有break和continue关键字,scala.util.control.Breaks类提供了类似的功能。

case语句中匹配多个条件

scala> val i = 5
i: Int = 5

scala> i match {
     | case 1 | 3 | 5 | 7 | 9 => println("odd")
     | case 2 | 4 | 6 | 8 | 10 => println("even")
     | }
odd

try/catch匹配一个或者多个异常

scala> val s = "foo"
s: String = foo

scala> try{
     | val i = s.toInt
     | }catch{
     | case e:Exception => e.printStackTrace
     | }
java.lang.NumberFormatException: For input string: "foo"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at scala.collection.immutable.StringLike$class.toInt(StringLike.scala:272)
    at scala.collection.immutable.StringOps.toInt(StringOps.scala:29)
    at $line124.$read$$iw$$iw$.liftedTree1$1(<console>:14)
    at $line124.$read$$iw$$iw$.<init>(<console>:13)
    at $line124.$read$$iw$$iw$.<clinit>(<console>)
    at $line124.$eval$.$print$lzycompute(<console>:7)
    at $line124.$eval$.$print(<console>:6)
    at $line124.$eval.$print(<console>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:786)
    at scala.tools.nsc.interpreter.IMain$Request.loadAndRun(IMain.scala:1047)
    at scala.tools.nsc.interpreter.IMain$WrappedRequest$$anonfun$loadAndRunReq$1.apply(IMain.scala:638)
    at scala.tools.nsc.interpreter.IMain$WrappedRequest$$anonfun$loadAndRunReq$1.apply(IMain.scala:637)
    at scala.reflect.internal.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:31)
    at scala.reflect.internal.util.AbstractFileClassLoader.asContext(AbstractFileClassLoader.scala:19)
    at scala.tools.nsc.interpreter.IMain$WrappedRequest.loadAndRunReq(IMain.scala:637)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:569)
    at scala.tools.nsc.interpreter.IMain.interpret(IMain.scala:565)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:807)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:825)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:825)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:825)
    at scala.tools.nsc.interpreter.ILoop.interpretStartingWith(ILoop.scala:825)
    at scala.tools.nsc.interpreter.ILoop.command(ILoop.scala:681)
    at scala.tools.nsc.interpreter.ILoop.processLine(ILoop.scala:395)
    at scala.tools.nsc.interpreter.ILoop.loop(ILoop.scala:415)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply$mcZ$sp(ILoop.scala:923)
	at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:909)
    at scala.tools.nsc.interpreter.ILoop$$anonfun$process$1.apply(ILoop.scala:909)
    at scala.reflect.internal.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:97)
    at scala.tools.nsc.interpreter.ILoop.process(ILoop.scala:909)
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:74)
    at scala.tools.nsc.MainGenericRunner.run$1(MainGenericRunner.scala:87)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:98)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:103)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14775次
    • 积分:377
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    最新评论