Here is a 2D array. This is a more efficient, low-level representation of two-dimensional data. We create the array with the Array.ofDim function.
object Program {
def main(args: Array[String]): Unit = {
// Create a 2D array of 3 rows and 3 columns.
val numbers = Array.ofDim[Int](3, 3)
// Update first row cells.
val result1 = numbers.apply(0)
result1.update(0, 900)
result1.update(1, 800)
result1.update(2, 700)
// Update second row.
val result2 = numbers.apply(1)
result2.update(0, -1)
// Update third row.
val result3 = numbers.apply(2)
result3.update(0, -2)
// Loop over rows.
for (row <- numbers) {
// Loop over cells in rows.
// ... Display all numbers on lines.
for (cell <- row) {
print(cell)
print(
" ")
}
println()
}
}
}
900 800 700
-1 0 0
-2 0 0