-1

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)
  • 2
    Please show the complete code – Talha Tayyab Nov 18 '21 at 07:26
  • My guess it's related to previous question that was posted https://stackoverflow.com/questions/70015532/i-received-the-error-error-expected-class-or-object-definition-in-scala – Ivan Stanislavciuc Nov 18 '21 at 07:40
  • Does this answer your question? [How do I print my Java object without getting "SomeType@2f92e0f4"?](https://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4) Note that, though the dupe target is for Java, it's really the same issue. The Java solutions explored in the dupe could be tailored to perhaps use some of Scala's advanced features. – President James K. Polk Dec 29 '22 at 16:21

2 Answers2

3

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.

Matthias Berndt
  • 4,387
  • 1
  • 11
  • 25
1

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

gatear
  • 946
  • 2
  • 10