Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print Two-Dimensional Array like table in scala

I'm having a problem with two dimensional array. I want the array to be like a table and not say Array (), Array().

Something like this :

 ........
 ........
 ........
 ........     
 ........
 ........
 ........
 ........
 ........

 scala> val table = Array.fill(9,8)('.')
 table: Array[Array[Char]] = Array(Array(., ., ., ., ., ., ., .), Array(., ., ., ., ., ., ., .),     
 Array(., ., ., ., ., ., ., .), Array(., ., ., ., ., ., ., .), Array(., ., ., ., ., ., ., .),   
 Array(., ., ., ., ., ., ., .), Array(., ., ., ., ., ., ., .), Array(., ., ., ., ., ., ., .),   
 Array(., ., ., ., ., ., ., .))
like image 838
acolisto Avatar asked Nov 18 '14 18:11

acolisto


1 Answers

You can use print, println, and foreach for the desired effect:

table foreach { row => row foreach print; println }

You can also use the mkString method on collections, which joins the elements of the list (either with no delimeter, or with an overload that provides a string delimeter):

print(table.map(_.mkString).mkString("\n"))
like image 69
Ben Reich Avatar answered Sep 20 '22 18:09

Ben Reich