74

It seems like the support for printing arrays is somewhat lacking in Scala. If you print one, you get the default garbage you'd 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 

Furthermore, you cannot use the Java toString/deepToString methods from the java.util.Arrays class: (or at least I cannot 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 I could find for printing a 2D array is to do the following:

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

Is there a more idiomatic way of doing this?

8 Answers 8

114

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

 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 IndexedSeq returned does have a stringprefix 'Array' by default, so I'm not sure whether this gives precisely what you wanted.

Sign up to request clarification or add additional context in comments.

1 Comment

Or use array.deep.foreach(println) if it could be a big array
39

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)) 

1 Comment

This is awesome! Exactly like the console. By the way, do you know how to print the type part, for example, "Array[Array[Int]] "
17

Adding little more to Arjan's answer - you can use the mkString method to print and even specify the separator between elements. For instance :

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>" 

Comments

6

Try simply this:

 // 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) 

Comments

3

I rather like this one:

Array(1, 7, 2, 9).foreach(println) 

Comments

2

You can get neat formatting of Array[Array[Somethings]] with custom separators for the inner as well as the outer array follows:

 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)) 

This results in:

 {{0,0}, {0,0}} 

Comments

1

The "functional programming" way to do this (as far as I concern) is:

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

Or if you don't really care about the spacing:

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 make this, I'd say just go with the imperative way.

1 Comment

it looks a bit "imperative" with all that foreaches more than functional.
0
Array(1, 7, 2, 9) foreach println 

Minor modification of rupert160's answer. No need for dots or parenthesis.

3 Comments

How is this different from the answer rupert160 gave over 2 years ago?
@jwvh no dots or parenthesis needed
And yet such important and noteworthy information was not included in your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.