复制二维数组

4

我想要复制一个二维数组。我想通过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
    }

}

3
在Scala中,for结构的第一个部分是一个推导式(comprehension),而不是一个循环。 - korefn
1
在Scala、Java和JVM中,只有一维数组。而数组的数组并不是多维数组。如果您需要它们,您需要从可用的线性“Array”创建它们。 - Randall Schulz
6个回答

7
你也可以使用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) 
}

scala> val aa = Array(Array(1, 2, 3), Array(0, 4, 2)) aa: Array[Array[Int]] = Array(Array(1, 2, 3), Array(0, 4, 2))scala> aa.clone res0: Array[Array[Int]] = Array(Array(1, 2, 3), Array(0, 4, 2))scala> res0(0)(0) = 7scala> res0 res2: Array[Array[Int]] = Array(Array(7, 2, 3), Array(0, 4, 2))scala> aa res3: Array[Array[Int]] = Array(Array(7, 2, 3), Array(0, 4, 2)) - Andriy Plokhotnyuk
1
那么就使用.. aa.map(_.clone) :) - Shrey
如果您不想使用 clone,那么可以使用 build map (_ map identity) - Luigi Plinge

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

1
您也可以使用方法Array.tabulate
val source = Array.fill[Int](5, 5)(1)
val target = Array.tabulate[Int](5, 5)((x, y) => source(x)(y))

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)

0

复制二维数组最简单的方法是使用array.clone,但如果你想通过for循环自己实现,可以使用以下代码:

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


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

是的,与当前接受的版本相比更有效率。 - Andriy Plokhotnyuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接