简体   繁体   English

在java中复制多维数组

[英]Replicate multidimensional array in java

In Java, how do we make a copy of a 3-D array?在 Java 中,我们如何制作 3-D 数组的副本? Thing is, when we use new_array.clone() or something of that sort, we are putting the address of the entry into the another_array, and not the actual value. new_array.clone()是,当我们使用new_array.clone()或类似的东西时,我们将条目的地址放入 another_array 中,而不是实际值。 Hence when i clear() old_array , new_array is empty too因此,当我 clear() old_arraynew_array也是空的

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

}

In Java, Objects are always referenced so when you do:在 Java 中,对象总是被引用,所以当你这样做时:

   moves1.add(mid_array);

It means mid_array is added to moves1 but still referenced with mid_array .这意味着mid_array已添加到moves1但仍由mid_array引用。 This way, when you call mid_array.clear() , its clear from both the places.这样,当您调用mid_array.clear() ,两个地方都清楚。

If you want to maintain the list inside moves1 , then better to create a new instance of mid_array inside the for loop eg below:如果你想在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
 }

This produces a real copy, inclusive copying all Elements.这会产生一个真正的副本,包括复制所有元素。

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

It uses this cloning library to copy the elements.它使用这个克隆库来复制元素。

Use it as follows:使用方法如下:

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

Generally you have to do deep copies manually (unless the object supports it for you, the documentation would tell you).通常你必须手动进行深拷贝(除非对象支持你,文档会告诉你)。 In this case you would need to use two nested loops and clone the bottommost ArrayList .在这种情况下,您需要使用两个嵌套循环并克隆最底层的ArrayList

You can create subclass of ArrayList and override the clone() method to perform deep copy instead of shallow copy.您可以创建 ArrayList 的子类并覆盖 clone() 方法来执行深拷贝而不是浅拷贝。 Eg:例如:

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

Once you have this you only need to do一旦你有了这个,你只需要做

myArray.clone()

Well you can do it the ugly way if you want.好吧,如果你愿意,你可以用丑陋的方式来做。 I forget if there is a function to do it but if you want to manually do it here is the way.我忘记是否有一个功能可以做到这一点,但如果你想手动完成,这里是方法。

 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;

This is just an example and probably isn't correct syntax but this way should work.这只是一个例子,可能不是正确的语法,但这种方式应该有效。 The i< 10 or any of the others should actually be i< length of the array dimension. i< 10 或其他任何一个实际上应该是 i< 数组维度的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM