I think this problem stems from my lack of understanding of Java.
I’d like to print the chars of a string which I’m guessing is some sort of funny java string.
I was given an object of type InputStream an needed to read the lines and parse them by a particular rule.
I used
val lines = Source.createBufferedSource(s).getLines()
to apparently get an Iterator for the lines.
Next, I used the following to get what IntelliJ thinks is an object of type Iterator[Array[String]]
val plists = lines.map(line => line.split(",").map(_.trim)).filter(list => ! list.contains(""))
When I try to print this with println, it prints as something I don’t understand.
println("slists="+slists.toList)
The output is the following.
slists=List([Ljava.lang.String;@49c7b90e)
Can someone tell me what this means? I notice the opening [ is never closed. Is the type name really “[Ljava.lang.string”? Is there a way to print the actual strings of characters?
It’s just what the toString method of a JVM array returns.
The default implementation of toString that gets inherited from java.lang.Object is more or less equal to this:
The class name of a String array is [Ljava.lang.String;. [ means it’s an array, L means it can contain references to objects, and java.lang.String means all those objects should be instances of java.lang.String. The ; is just because Java loves its semicolons, I suppose.
If you want to pretty print an array you can use java.util.Arrays.toString.
val list = List(Array(1,2),Array(3,4))
println( list.map(java.util.Arrays.toString) ) // List([1, 2], [3, 4])
When you play around with arrays in the Scala REPL, most of the time you won’t see things like [Ljava.lang.String;@49c7b90e. That’s because the REPL contains special cases for when the results of expressions contain arrays.