如何在Play框架中将二维字符串数组从Java代码传递到scala.html?

时间:2019-04-24 12:18:22

标签: java html scala playframework

我有一个问题,在我为所有初始化初始化字符串数组"'[' expected but integer literal found"时,它给我一个错误"arr[i][j]="string";"。我想在HTML视图的表格中显示此 2D数组

1 个答案:

答案 0 :(得分:0)

由于此answer指定了Java数组映射,因此直接映射成Scala个数组。 而且,在Scala中,要访问collection中的元素,您需要使用apply()方法,该方法等效于()


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"

这是更新元素的方法:

arr(1)(1) = "string"

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