2016-03-23 85 views
-1

我有這樣的Scala代碼:斯卡拉數組ArrayIndexOutOfBoundsException異常:

val line = "DB:move" 
val str = line.split(":")  
println(line.split(':')(1)) 

當指數是一個1得到

java.lang.ArrayIndexOutOfBoundsException: 1 

,但是當我把指數作爲0沒有任何問題的。

+1

可以打印您的行嗎? – eliasah

+0

你一定在開玩笑吧。該行分割不能產生該錯誤! – eliasah

+0

對我很好:'''val line =「DB:move」 val str = line.split(「:」) println(line.split(':')(1)) //退出粘貼模式,現在解釋。 move line:String = DB:move str:Array [String] = Array(DB,move)'''' –

回答

0

當你分割上的字符/字符串,是不是在該字符串你會得到一個包含單個元素的數組一個字符串 - 初始字符串:

scala> "asdf".split(":") 
res0: Array[String] = Array(asdf) 

數組是基於0的,所以訪問索引0處的第一個元素很好,但是會索引索引1.