简体   繁体   中英

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
    }

}

you can use clone method as well !!

def copy(bild: Array[Array[Int]]): Unit = {
    val copy = bild.clone
} 

Update :

since, Array[Int] is still mutable references, cloning will still not solve the problem.. as mentioned by Andriy Plokhotnyuk in his comment..

Problem :

val og = Array(Array(1, 2, 3), Array(4,5,6))      //> og  : Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6))
val copy = og.clone                               //> copy  : Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6))
copy(0)(0) = 7
og                                                //> res2: Array[Array[Int]] = Array(Array(7, 2, 3), Array(4, 5, 6))
copy                                              //> res3: Array[Array[Int]] = Array(Array(7, 2, 3), Array(4, 5, 6))

Here any update to copy will reflect to og also..

Sol :

So I primarily need to clone Array[Int] as well.. hence..

val og = Array(Array(1, 2, 3), Array(4,5,6))      //> og  : Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6))
val copy = og.map(_.clone)                        //> copy  : Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6))
copy(0)(0) = 7
og                                                //> res2: Array[Array[Int]] = Array(Array(1, 2, 3), Array(4, 5, 6))
copy                                              //> res3: Array[Array[Int]] = Array(Array(7, 2, 3), Array(4, 5, 6))

Hence.. Refactoring copy method in the question to..

def copy(bild: Array[Array[Int]]): Unit = {
    val copy = bild.map(_.clone) 
}
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)
    }
  }
}

Quite simple:

scala> Array.ofDim[Int](3)
//res0: Array[Int] = Array(0, 0, 0)
scala> Array.ofDim[String](4)
//res1: Array[String] = Array(null, null, null, null)

You can also use the method Array.tabulate :

val source = Array.fill[Int](5, 5)(1)
val target = Array.tabulate[Int](5, 5)((x, y) => source(x)(y))

The easiest way to copy a 2-dimensional Array is array.clone , but if you want to do it on your own via for-comprehension you can use the following code:

def copy[A](arr: Array[Array[A]]) = for (x <- 0 until arr.length) yield for (y <- 0 until arr(x).length) yield arr(x)(y)

This will give you a Vector[Vector[A]] .

More efficient for large arrays:

scala> :paste
// Entering paste mode (ctrl-D to finish)

def copyOf(as: Array[Array[Int]]): Array[Array[Int]] = {
  val cas = Array.ofDim[Array[Int]](as.length)
  for(i <- 0 until as.length) {
    val a = as(i)
    cas(i) = java.util.Arrays.copyOf(a, a.length)
  }
  cas
}

// Exiting paste mode, now interpreting.

copyOf: (as: Array[Array[Int]])Array[Array[Int]]

scala> Array(Array(0, 1), Array(2, 3))
res0: Array[Array[Int]] = Array(Array(0, 1), Array(2, 3))

scala> copyOf(res0)
res1: Array[Array[Int]] = Array(Array(0, 1), Array(2, 3))

scala> res0(0)(0) = 7

scala> res0
res3: Array[Array[Int]] = Array(Array(7, 1), Array(2, 3))

scala> res1
res4: Array[Array[Int]] = Array(Array(0, 1), Array(2, 3))

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