beatsspam beatsspam - 2 months ago 7
Scala Question

Scala Multi-Dimensional Arrays not work

val matrix = Array.ofDim[Int](r,c);
//matrix(0)(0) = 0;

for (i <- 0 to r) {
//println(i)
for (j <- 0 to c){
if (j == 0)
matrix(i)(j) = 1
}
}

Answer

Your Array has r rows and c cols, numbered from 0. You're trying to access an element out of those bounds when i == r or j == c.

for (i <- 0 until r) {
  for (j <- 0 until c) {
    ...
  }
}