0

I'm trying to use method split on string line like this:

println("/gg/dsa".split('/').toString)
println("/gg/dsa".split("/").toString)

But both methods prints something strange like this:

[Ljava.lang.String;@6ac7c6cc

But as I can see in examples http://alvinalexander.com/scala/scala-split-string-example this should print array data. What I've missed here?

sergeda
  • 2,061
  • 3
  • 20
  • 43
  • Possible duplicate of [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – marcospereira Mar 03 '16 at 14:51
  • @marcospereira Well, split in Scala returns Scala array and it is not obvious for new comers that it actually use Java array with different behavior of toString than other Scala collections like List, Map, etc. – sergeda Mar 05 '16 at 03:26

3 Answers3

1

you have some options to do that:

as described previously:

println("/gg/dsa".split('/').mkString(", "))

will print:

, gg, dsa

a valid alternative with the same output can be:

"/gg/dsa".split('/').foreach(print)

another alternative:

runtime.ScalaRunTime.stringOf("/gg/dsa".split('/'))

with the output:

Array("", gg, dsa)

drstein
  • 1,113
  • 1
  • 10
  • 24
0

Its cause you convert it to a string the String represention is printed. Now if you want to prettify an array just do:

println("/gg/dsa".split('/').deep.mkString("\n"))
Kordi
  • 2,405
  • 1
  • 14
  • 13
0

This is the definition for java.lang.Object#toString:

public String toString() {
   return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

java.lang.Array inherits directly from java.lang.Object without overriding toString to return something more appropriate, resulting in the output you're getting. What you can do is call .mkString(", ") in Scala to create the result string:

println("/gg/dsa".split("/").mkString(", "))

Which will result in output like:

, gg, dsa

David Xu
  • 5,555
  • 3
  • 28
  • 50