复制二维数组

时间:2013-03-28 12:44:51

标签: arrays scala for-loop

我想要复制一个二维数组。我想用for循环做这个,我知道怎么做,但我不能完成其余的。

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
    }

}

6 个答案:

答案 0 :(得分:6)

你也可以使用clone方法!!

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

更新:

从那以后,Array [Int]仍然是可变引用,克隆仍然无法解决问题。 正如Andriy Plokhotnyuk在评论中提到的那样......

问题:

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))

此处copy的任何更新都会反映到og ..

Sol:

所以我主要需要克隆Array [Int] ..因此..

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))

因此..将问题中的复制方法重构为..

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

答案 1 :(得分:2)

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)
    }
  }
}

答案 2 :(得分:1)

非常简单:

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)

答案 3 :(得分:1)

您还可以使用方法Array.tabulate

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

答案 4 :(得分:0)

复制二维数组的最简单方法是array.clone,但是如果您想通过自我理解这样做,可以使用以下代码:

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)

这会给你一个Vector[Vector[A]]

答案 5 :(得分:0)

对大型阵列更有效:

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))