为什么无法访问String值?
我希望s1是“a”而不是它的Ljava.lang.String; @ d70d7a?
val it = Iterator("(a,((a,b),1.0))") //> it : Iterator[String] = non-empty iterator
val s1 = it.next.replace("(" , "").replace(")" , "").split(",").toString.split(",")
//> s1 : Array[String] = Array([Ljava.lang.String;@d70d7a)
println("s1 is "+s1(0)) //> s1 is [Ljava.lang.String;@d70d7a
答案 0 :(得分:3)
让我们逐个命令:
val it = Iterator("(a,((a,b),1.0))")
// here we got iterator on one String
val s1 = it.next // "(a,((a,b),1.0))"
.replace("(" , "") // "a,a,b),1.0))"
.replace(")" , "") // "a,a,b,1.0"
.split(",") // multiple lines in array: "a", "a","b","1.0"
.toString // Array[String].toString returns what you got: Ljava.lang.String;@d70d7a
.split(",") // one String (because there's no "," signs)
也许您应该在toList
之前运行toString
,因为toString
的定义方式是您希望在List
的此实现中定义它:
val s1 = ...
.split(",")
.toList
.toString
...
也许您应该查看Java: split() returns [Ljava.lang.String;@186d4c1], why?以获得澄清。
答案 1 :(得分:2)
.split(",")
使得数组和.toString
无法处理数组,之后您再次按.split(",")
分割
我认为没有用。
您还可以使用replaceAll
代替多个replace
scala> val it = Iterator("(a,((a,b),1.0))")
it: Iterator[String] = non-empty iterator
scala> val s1 = it.next.replaceAll("[()]" , "").split(",")
s1: Array[String] = Array(a, a, b, 1.0)
scala> println("s1 is "+s1(0))
s1 is a