Home > front end >  Why does println(array) have strange output in Scala?
Why does println(array) have strange output in Scala?

Time:11-23

when I use println to print the content of an Array in Scala I received some strange results. I know how fix this in java. but the solution in java gives me an error in scala. the output of println is something like:

List([I@5ec77191, [I@4642b71d, [I@1450078a)

CodePudding user response:

Scala's Array type is exactly the same as Java's, so if you call toString on it, that is what you get. An easy way to print the contents is to convert the arrays to another type of collection first, e. g. Vector or ArraySeq.

CodePudding user response:

Collections in Scala have mkString . This applies for Array.

val arr: Array[String] = 
 ???
val arrs: Array[Array[String]] = 
 ???

println(arr.mkString(",")) //print the contents of just one array

arrs
  .foreach(arr => println(arr.mkString(",")) //print something similar to your example

  • Related