Joe Wash Joe Wash - 2 months ago 20
Scala Question

copy 2-dimensional Array

I want to copy a 2-dimensional Array. I want to do this with for-loops and I have a idea how to do so, but I can't complete the rest.

def copy(bild:Array[Array[Int]]):Unit = {

for(x <- 0 until bild.length)
for(y <- 0 until bild(x).length) {
bild(x)(y) =
//i don't know how to create the new array
}

}

Answer
def copy(bild: Array[Array[Int]]):Unit = {
  val result = Array.ofDim[Array[Int]](bild.length)
  for(x <- 0 until bild.length) {
    result(x) = Array.ofDim[Int](bild(x).length)
    for(y <- 0 until bild(x).length) {
      result(x)(y) = bild(x)(y)
    }
  }
}
Comments