简体   繁体   中英

How to pass a 2 dimensional string array from java code to scala.html in play framework?

I have a problem where it gives me an error "'[' expected but integer literal found" at initializing the array the string array "arr[i][j]="string";" for all my initialization. I want to display this 2D array in a table in the HTML view.

As this answer specifies Java array maps directly into Scala ones. And also, in Scala , to access an element in a collection , you need to use the apply() method, which is equivalent to the () :


val arr = Array.fill(3,3)("hello")
// Displays
//arr: Array[Array[String]] = Array(
//  Array("hello", "hello", "hello"),
//  Array("hello", "hello", "hello"),
//  Array("hello", "hello", "hello")
//)

val row0 = arr.apply(0)
//Displays
//row0: Array[String] = Array("hello", "hello", "hello")

val row1 = arr(1)
//Display
//row1: Array[String] = Array("hello", "hello", "hello")

val elem11 = row1(1)
//elem11: String = "hello"

Here is how you can update an element:

arr(1)(1) = "string"

arr.map(_.mkString(",")).mkString("\n")
// Displays
//res9: String = """hello,hello,hello
//hello,string,hello
//hello,hello,hello"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM