为什么新的失败?

时间:2013-05-12 10:40:59

标签: scala scala-collections

在scala中,这没关系

val v = Vector(1,2,3)

这不行

val v = new Vector(1,2,3); 

你得到:

java.lang.NullPointerException
                                                  //|   at scala.collection.immutable.VectorIterator.next(Vector.scala:660)
                                                  //|   at scala.collection.Iterator$$anon$10.next(Iterator.scala:312)
                                                  //|   at scala.collection.Iterator$$anon$11.next(Iterator.scala:328)
                                                  //|   at scala.collection.Iterator$class.foreach(Iterator.scala:727)
                                                  //|   at scala.collection.AbstractIterator.foreach(Iterator.scala:1157)
                                                  //|   at scala.

为什么?

1 个答案:

答案 0 :(得分:6)

查看api http://www.scala-lang.org/api/current/index.html#scala.collection.immutable.Vector

您正在尝试调用如下所示的构造函数:

  

new Vector(startIndex:Int,endIndex:Int,focus:Int)

用这个:

  

val v = new Vector(1,2,3);

看起来它试图在3处达到指数,但显然向量不够长