2012-11-28 77 views
-1

在Java中,我们如何复制一个3-D数组? 事情是,当我们使用new_array.clone()或类似的东西时,我们将条目地址放入another_array中,而不是实际值。 因此,当我清除()old_arraynew_array是空太在java中复制多维数组

private List<List<List<String>>> moves = new ArrayList<List<List<String>>>(); 
private List<List<List<String>>> moves1 = new ArrayList<List<List<String>>>(); 

.blah 
.blah 
.blah 

mid_array = new ArrayList<List<String>>();//will use this array to insert inot moves1 

for(int f = 0; f < moves.size(); f++)//make a copy of original array. 
{ 

    List<String> row_st = moves.get(f).get(0); 
    List<String> deck_st = moves.get(f).get(1); 

    mid_array.add(row_st);//current array of the Rows 
    mid_array.add(deck_st);//current array of the Deck 

    moves1.add(mid_array); 

    System.out.println("Moves1 "+moves1);//displays the new array correctly 

    mid_array.clear();//clear mid_array, NOT moves1 or moves arrays 

    System.out.println("Moves1 "+moves1);//new array is now empty 

} 

回答

3

在Java中,对象总是引用,所以当你做:

moves1.add(mid_array); 

这意味着mid_array添加到moves1mid_array仍引用。这样,当你打电话给mid_array.clear()时,从两个地方都清楚了。

如果您想维护moves1内的列表,则最好在for环内创建mid_array的新实例,例如,如下:

for(int f = 0; f < moves.size(); f++)//make a copy of original array. 
{ 
    List<List<String>> mid_array = new ArrayList<List<String>>(); 
    List<String> row_st = moves.get(f).get(0); 
    List<String> deck_st = moves.get(f).get(1); 

    mid_array.add(row_st);//current array of the Rows 
    mid_array.add(deck_st);//current array of the Deck 

    moves1.add(mid_array); 

    System.out.println("Moves1 "+moves1);//displays the new array correctly 

    //No need to clear as in each iteration, it will instantiate a new mid_array 
} 
0

通常你必须做手工深层副本(除非对象支持它为你的文档会告诉你)。在这种情况下,您需要使用两个嵌套循环并克隆最底层的ArrayList

-3

那么你可以做到丑陋的方式,如果你想。我忘了是否有一个功能来做到这一点,但如果你想手动做到这一点的方式。

int array[10][10][10] = ...// just assume they are all filled. 
    int newArray[10][10][10]; 
     for(int i = 0; i< 10; i++) 
     { 
      for(int j = 0; j< 10; j++) 
      { 
       for(int k = 0; k< 10; k++) 
       { 
        newArray[i][j][k] = array[i][j][k]; 
       } 
      } 
     } 

    array = NULL; 

这只是一个例子,可能是不正确的语法,但这种方式应该工作。我或其他任何其他人应该实际上是我的<阵列维度的长度。

-1

您可以创建ArrayList的子类并覆盖clone()方法以执行深度复制而不是浅度复制。例如:

public class DeepCopyArrayList extends ArrayList 
{ 
    public Object clone() 
    { 
    // implement deep copy clone here 
    } 
} 

一旦你有了这个,你只需要做

myArray.clone() 
2

这将产生一个真正的副本,包括复制所有元素。

public static <T> List<List<List<T>>> list3DCopy(List<List<List<T>>> source) { 
    List<List<List<T>>> result = new ArrayList<>(source.size()); 
    Cloner cloner = new Cloner(); 
    for(List<List<T>> innerList : source) { 
     List<List<T>> copy = new ArrayList<>(innerList.size()); 
     for (List<T> innerInnerList : innerList) { 
      List<T> innerCopy = new ArrayList<>(innerInnerList.size()); 
      for (T item : innerInnerList) { 
       T clone = cloner.deepClone(item); 
       innerCopy.add(clone); 
      } 
      copy.add(innerCopy); 
     } 
     result.add(copy); 
    } 
    return result; 
} 

它使用this cloning library复制元素。

使用它,如下所示:

List<List<List<YourClass>>> original = ... 
List<List<List<YourClass>>> copy = list3DCopy(original);