在 Java 中,我们如何制作 3-D 数组的副本?问题是,当我们使用new_array.clone()
或类似的东西时,我们将条目的地址放入另一个数组,而不是实际值。因此,当我 clear() 时old_array
,new_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
}