Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing array in Scala

Tags:

arrays

scala

I am having problem with most basic Scala operation and it is making me crazy.

val a = Array(1,2,3)  println(a)   and result is [I@1e76345  println(a.toString()) and result is [I@1e76345  println(a.toString) and result is [I@1e76345 

Can anyone tell me how to print array without writing my own function for doing that because that is silly. Thanks!

like image 547
Ivan Longin Avatar asked Jul 13 '13 21:07

Ivan Longin


People also ask

What is Array in Scala?

Array is a special kind of collection in scala. it is a fixed size data structure that stores elements of the same data type. The index of the first element of an array is zero and the last element is the total number of elements minus one. It is a collection of mutable values.

What is mkString in Scala?

The mkString() method is utilized to display all the elements of the list in a string along with a separator. Method Definition: def mkString(sep: String): String. Return Type: It returns all the elements of the list in a string along with a separator. Example #1: // Scala program of mkString()


1 Answers

mkString will convert collections (including Array) element-by-element to string representations.

println(a.mkString(" ")) 

is probably what you want.

like image 62
Rex Kerr Avatar answered Sep 28 '22 01:09

Rex Kerr