编写一个复制数组的泛型方法
Write a generic method to copy an array
对于我的编程任务,我被要求编写一个通用的复制方法来从一个数组复制到一个相同大小和类型的数组。这在 Java 中甚至可能吗?我尝试的所有操作都以 "generic array creation" 错误告终。我迷路了,不知道如何解决这个问题!
public class copyArray<AnyType>{
public copyArray(AnyType[] original){
AnyType[] newarray = new AnyType[original.length];
for(int i =0; i<original.length; i++){
newarray[i] = original[i]; }
}
您可以使用反射的概念来编写一个可以在运行时确定类型的通用复制方法。简而言之,反射是一种在运行时检查 classes、接口、字段和方法的能力,而无需在编译时知道 classes、方法等的名称。
java.lang.Reflect together with java.lang.Class 包含 Java 反射 API。这个方法使用了这两个 classes 和它们的一些方法来创建一个通用的 arrayCopy
方法来为我们找出类型。
更多信息:What is reflection and why is it useful?
可能不熟悉的语法
Class<?>
使用通配符运算符 ?
基本上说我们可以有一个 Class
未知类型的对象 - [=55 的通用版本=] Class
.
<T>
是代表 raw type 的通用运算符
Array
数组 class 提供静态方法来动态创建和访问 Java 数组。即 class 包含允许您设置和查询数组元素的值、确定数组的长度以及创建数组的新实例的方法。我们将使用 Array.newInstance()
来自反射的方法API
getClass ()
- returns 一个包含 Class 对象的数组,表示所有 public classes 和接口的成员表示 class 个对象。
getComponentType()
- returns class 表示数组的组件类型(什么类型即 int, 等)。
newInstance()
- 获取数组的新实例。
private <T> T[] arrayCopy(T[] original) {
//get the class type of the original array we passed in and determine the type, store in arrayType
Class<?> arrayType = original.getClass().getComponentType();
//declare array, cast to (T[]) that was determined using reflection, use java.lang.reflect to create a new instance of an Array(of arrayType variable, and the same length as the original
T[] copy = (T[])java.lang.reflect.Array.newInstance(arrayType, original.length);
//Use System and arraycopy to copy the array
System.arraycopy(original, 0, copy, 0, original.length);
return copy;
}
更简单的解决方案是使用任何数组的现有 clone()
方法(除非您的作业明确告诉您不要使用 clone()
):
public static <T> T[] arrayCopy(T[] original) {
return original.clone();
}
其实你根本不需要这样的通用辅助方法,你可以直接调用clone()
方法(它也适用于原始数组)。
对于我的编程任务,我被要求编写一个通用的复制方法来从一个数组复制到一个相同大小和类型的数组。这在 Java 中甚至可能吗?我尝试的所有操作都以 "generic array creation" 错误告终。我迷路了,不知道如何解决这个问题!
public class copyArray<AnyType>{
public copyArray(AnyType[] original){
AnyType[] newarray = new AnyType[original.length];
for(int i =0; i<original.length; i++){
newarray[i] = original[i]; }
}
您可以使用反射的概念来编写一个可以在运行时确定类型的通用复制方法。简而言之,反射是一种在运行时检查 classes、接口、字段和方法的能力,而无需在编译时知道 classes、方法等的名称。
java.lang.Reflect together with java.lang.Class 包含 Java 反射 API。这个方法使用了这两个 classes 和它们的一些方法来创建一个通用的 arrayCopy
方法来为我们找出类型。
更多信息:What is reflection and why is it useful?
可能不熟悉的语法
Class<?>
使用通配符运算符 ?
基本上说我们可以有一个 Class
未知类型的对象 - [=55 的通用版本=] Class
.<T>
是代表 raw typeArray
数组 class 提供静态方法来动态创建和访问 Java 数组。即 class 包含允许您设置和查询数组元素的值、确定数组的长度以及创建数组的新实例的方法。我们将使用 Array.newInstance()
来自反射的方法API
getClass ()
- returns 一个包含 Class 对象的数组,表示所有 public classes 和接口的成员表示 class 个对象。getComponentType()
- returns class 表示数组的组件类型(什么类型即 int, 等)。newInstance()
- 获取数组的新实例。private <T> T[] arrayCopy(T[] original) {
//get the class type of the original array we passed in and determine the type, store in arrayType
Class<?> arrayType = original.getClass().getComponentType();
//declare array, cast to (T[]) that was determined using reflection, use java.lang.reflect to create a new instance of an Array(of arrayType variable, and the same length as the original
T[] copy = (T[])java.lang.reflect.Array.newInstance(arrayType, original.length);
//Use System and arraycopy to copy the array
System.arraycopy(original, 0, copy, 0, original.length);
return copy;
}
更简单的解决方案是使用任何数组的现有 clone()
方法(除非您的作业明确告诉您不要使用 clone()
):
public static <T> T[] arrayCopy(T[] original) {
return original.clone();
}
其实你根本不需要这样的通用辅助方法,你可以直接调用clone()
方法(它也适用于原始数组)。