如何在Java中进行2d数组的深度复制?
我刚刚在2d如何在Java中进行2d数组的深度复制?,java,arrays,multidimensional-array,copy,deep-copy,Java,Arrays,Multidimensional Array,Copy,Deep Copy,我刚刚在2d布尔值数组中使用.clone()得到了一个比特,认为这是一个深度拷贝 如何执行我的boolean[][]数组的深度复制 我是否应该遍历它并执行一系列的System.arraycopy?是的,您应该迭代二维布尔数组以进行深度复制。如果您使用的是Java6,还可以查看java.util.array#copyOf方法 我建议使用Java 6的下一个代码: public static boolean[][] deepCopy(boolean[][] original) { if (o
布尔值数组中使用.clone()
得到了一个比特,认为这是一个深度拷贝
如何执行我的boolean[][]
数组的深度复制
我是否应该遍历它并执行一系列的System.arraycopy
?是的,您应该迭代二维布尔数组以进行深度复制。如果您使用的是Java6,还可以查看java.util.array#copyOf
方法
我建议使用Java 6的下一个代码:
public static boolean[][] deepCopy(boolean[][] original) {
if (original == null) {
return null;
}
final boolean[][] result = new boolean[original.length][];
for (int i = 0; i < original.length; i++) {
result[i] = Arrays.copyOf(original[i], original[i].length);
// For Java versions prior to Java 6 use the next:
// System.arraycopy(original[i], 0, result[i], 0, original[i].length);
}
return result;
}
publicstaticboolean[]deepCopy(boolean[]original){
如果(原始==null){
返回null;
}
最终布尔值[][]结果=新布尔值[original.length][];
对于(int i=0;i
是的,这是唯一的方法。java.util.Arrays
和commons lang都不提供数组的深度拷贝。我是数组实用程序的粉丝。它有一个copyOf方法,可以为您执行一维数组的深度复制,因此您可能需要如下内容:
//say you have boolean[][] foo;
boolean[][] nv = new boolean[foo.length][foo[0].length];
for (int i = 0; i < nv.length; i++)
nv[i] = Arrays.copyOf(foo[i], foo[i].length);
//假设您有布尔[][]foo;
布尔值[][]nv=新布尔值[foo.length][foo[0].length];
对于(int i=0;i
我已经找到了一个递归数组深度副本。即使对于具有不同维度长度的多维数组,它似乎也能很好地工作
private static final int[][][] INT_3D_ARRAY = {
{
{1}
},
{
{2, 3},
{4, 5}
},
{
{6, 7, 8},
{9, 10, 11},
{12, 13, 14}
}
};
下面是实用方法
@SuppressWarnings("unchecked")
public static <T> T[] deepCopyOf(T[] array) {
if (0 >= array.length) return array;
return (T[]) deepCopyOf(
array,
Array.newInstance(array[0].getClass(), array.length),
0);
}
private static Object deepCopyOf(Object array, Object copiedArray, int index) {
if (index >= Array.getLength(array)) return copiedArray;
Object element = Array.get(array, index);
if (element.getClass().isArray()) {
Array.set(copiedArray, index, deepCopyOf(
element,
Array.newInstance(
element.getClass().getComponentType(),
Array.getLength(element)),
0));
} else {
Array.set(copiedArray, index, element);
}
return deepCopyOf(array, copiedArray, ++index);
}
@SuppressWarnings(“未选中”)
公共静态T[]deepCopyOf(T[]数组){
如果(0>=array.length)返回数组;
返回(T[])深度副本(
阵列,
Array.newInstance(数组[0].getClass(),Array.length),
0);
}
私有静态对象deepCopyOf(对象数组、对象copiedArray、int索引){
if(index>=Array.getLength(Array))返回copiedArray;
Object元素=Array.get(数组,索引);
if(element.getClass().isArray()){
数组.set(复制数组、索引、深度复制)(
元素,
Array.newInstance(
元素.getClass().getComponentType(),
Array.getLength(元素)),
0));
}否则{
set(复制数组、索引、元素);
}
返回deepCopyOf(数组,copiedArray,++index);
}
编辑:更新了用于基本数组的代码。在Java 8中,这可以通过使用lambdas作为一行程序来完成:
<T> T[][] deepCopy(T[][] matrix) {
return java.util.Arrays.stream(matrix).map(el -> el.clone()).toArray($ -> matrix.clone());
}
T[]deepCopy(T[]matrix){
返回java.util.Arrays.stream(matrix.map(el->el.clone()).toArray($->matrix.clone());
}
这里有一个反射示例,使用它更健壮,更容易理解。此方法将复制任何数组,并深度复制多维数组
package mcve.util;
import java.lang.reflect.*;
public final class Tools {
private Tools() {}
/**
* Returns a copy of the specified array object, deeply copying
* multidimensional arrays. If the specified object is null, the
* return value is null. Note: if the array object has an element
* type which is a reference type that is not an array type, the
* elements themselves are not deep copied. This method only copies
* array objects.
*
* @param array the array object to deep copy
* @param <T> the type of the array to deep copy
* @return a copy of the specified array object, deeply copying
* multidimensional arrays, or null if the object is null
* @throws IllegalArgumentException if the specified object is not
* an array
*/
public static <T> T deepArrayCopy(T array) {
if (array == null)
return null;
Class<?> arrayType = array.getClass();
if (!arrayType.isArray())
throw new IllegalArgumentException(arrayType.toString());
int length = Array.getLength(array);
Class<?> componentType = arrayType.getComponentType();
@SuppressWarnings("unchecked")
T copy = (T) Array.newInstance(componentType, length);
if (componentType.isArray()) {
for (int i = 0; i < length; ++i)
Array.set(copy, i, deepArrayCopy(Array.get(array, i)));
} else {
System.arraycopy(array, 0, copy, 0, length);
}
return copy;
}
}
package mcve.util;
导入java.lang.reflect.*;
公共最终类工具{
专用工具(){}
/**
*返回指定数组对象的副本,并进行深度复制
*多维数组。如果指定的对象为null,则
*返回值为空。注意:如果数组对象有一个元素
*类型,该类型是不是数组类型的引用类型
*元素本身没有深度复制。此方法仅复制
*数组对象。
*
*@param array要深度复制的数组对象
*@param要深度复制的数组的类型
*@返回指定数组对象的副本,深度复制
*多维数组,如果对象为null,则为null
*@如果指定的对象不存在,则引发IllegalArgumentException
*阵列
*/
公共静态T阵列副本(T阵列){
if(数组==null)
返回null;
类arrayType=array.getClass();
如果(!arrayType.isArray())
抛出新的IllegalArgumentException(arrayType.toString());
int length=Array.getLength(数组);
类componentType=arrayType.getComponentType();
@抑制警告(“未选中”)
T copy=(T)Array.newInstance(componentType,length);
if(componentType.isArray()){
对于(int i=0;i
您可以迭代此数组并执行一系列方法调用:
boolean[]arr1={{true,true},{false,true};//原始数组
布尔值[][]arr2=Arrays.copyOf(arr1,arr1.length);//浅拷贝
布尔值[][]arr3=Arrays.stream(arr1)//深度复制
.map(arr->Arrays.copyOf(arr,arr.length))
.toArray(布尔[]]::新建);
arr1[0][0]=假;
System.out.println(Arrays.deepToString(arr1));//[[假,真],[假,真]]
System.out.println(Arrays.deepToString(arr2));//[[假,真],[假,真]]
System.out.println(Arrays.deepToString(arr3));//[true,true],[false,true]]
或者您可以调用以下方法:
boolean[]arr3=Arrays.stream(arr1)
.map(布尔值[]::克隆)
.toArray(布尔[]]::新建);
或者,您可以为此目的创建一个:
static T[]deepCopy(T[]matrix){
返回数组.stream(矩阵)
.map(arr->arr.clone())
.toArray(s->matrix.clone());
}
另请参见:编辑:发现自己:导入java.lang.reflect.Array代码>这个答案有一个好主意,但不幸的是,代码中存在一些问题,使得它不是一个很好的示例。(1) array[0].getClass()
使该方法不适用于混合对象的数组,例如newnumber[]{1,2.5}
。它应该使用array.getClass().getComponentType()
inst