【发布时间】:2015-05-02 14:54:09
【问题描述】:
我有以下代码。
import scala.collection.mutable.MutableList
val x = MutableList[Int]()
(1 to 10).foreach(x+=1)
我收到java.lang.IndexOutOfBoundsException: 1 错误。
但是,
(1 to 10).foreach(println) this does not throw any error.
indexOutOfBoundException 可以使用 lambda 运算符解决如下:
(1 to 10).foreach(_ => x+=1)
这一切都很好。
我的问题是:
1. 为什么我需要在第一种情况下使用 lambda 运算符而不是第二种情况?
2.为什么编译器会抛出IndexOutOfBoundException,我想这不是这个异常的正确上下文。
【问题讨论】:
标签: scala collections