Scala - Printed Arrays

It seems that print array support is somewhat missing in Scala. If you print it, you will get the default junk that you get in Java:

scala> val array = Array.fill(2,2)(0) array: Array[Array[Int]] = Array(Array(0, 0), Array(0, 0)) scala> println(array) [[I@d2f01d 

Also, you cannot use the Java toString / deepToString methods from the java.util.Arrays class: (or at least I can't figure it out)

 scala> println(java.util.Arrays.deepToString(array)) <console>:7: error: type mismatch; found : Array[Array[Int]] required: Array[java.lang.Object] println(java.util.Arrays.deepToString(array)) 

The best solution for printing a 2D array I can do the following:

 scala> println(array.map(_.mkString(" ")).mkString("\n")) 0 0 0 0 

Is there a more idiomatic way to do this?

+49
scala
Jul 25 '10 at 5:02
source share
6 answers

In Scala 2.8, you can use the deep method defined in Array, which returns an IndexedSeq that combines all (possibly nested) elements of this array and calls mkString:

 scala> val array = Array.fill(2,2)(0) array: Array[Array[Int]] = Array(Array(0, 0), Array(0, 0)) scala> println(array.deep.mkString("\n")) Array(0, 0) Array(0, 0) 

The return value of IndexedSeq has a default stringprefix value of 'Array', so I'm not sure if this gives exactly what you wanted.

+94
Jul 25 '10 at 5:16
source share

How about this:

 scala> val array = Array.fill(2,2)(0) array: Array[Array[Int]] = Array(Array(0, 0), Array(0, 0)) scala> import scala.runtime.ScalaRunTime._ import scala.runtime.ScalaRunTime._ scala> val str = stringOf(array) str: String = Array(Array(0, 0), Array(0, 0)) 
+32
Jul 25 '10 at 9:45
source share

By adding a bit more of Arjan's answer, you can use the mkString method to print and even specify a separator between the elements. For example:

 val a = Array(1, 7, 2, 9) a.mkString(" and ") // "1 and 7 and 2 and 9" a.mkString("<", ",", ">") //mkString(start: String, sep: String, end: String) // "<1,7,2,9>" 
+8
Sep 02 '15 at 5:15
source share

Try simply:

  // create an array val array1 = Array(1,2,3) // print an array elements seperated by comma println(array1.mkString(",")) // print an array elements seperated by a line println(array1.mkString("\n")) // create a function def printArray[k](a:Array[k])= println(a.mkString(",")) printArray(array1) 
+4
Apr 25 '17 at 14:10
source share

A way of "functional programming" for this (as far as I know):

 scala> array foreach{case a => a foreach {b => print(b.toString + " ")}; print('\n')} 0 0 0 0 

Or, if you care, that distance:

 scala> array foreach{a => a foreach println} 0 0 0 0 

IMHO, functional programming can get a little messy if it takes too long to do this, I would say, just go with the imperative way.

+1
Jul 27 '10 at 4:03 on
source share

You can get neat formatting of Array [Array [Somethings]] with custom separators for both the internal and external arrays:

  def arrayToString(a: Array[Array[Int]]) : String = { val str = for (l <- a) yield l.mkString("{", ",", "}") str.mkString("{",",\n","}") } val foo = Array.fill(2,2)(0) println(arrayToString(foo)) 

It leads to:

  {{0,0}, {0,0}} 
+1
Nov 26 '12 at 10:05
source share



All Articles