将原始 int 数组 class (int[].class) 传递给期望 Class<T[]> 的泛型方法无法编译
passing primitive int array class (int[].class) to a generic method which expects Class<T[]> fails to compile
我有以下代码:
public class Test {
private Map<String, Object> delegate;
public Test(Map<String, Object> delegate) {
this.delegate = delegate;
}
public <T> T[] getValueAsArray(String key, Class<T[]> arrayClz) {
T[] ret = null;
Object val = delegate.get(key);
if (val != null && arrayClz != null && arrayClz.isInstance(val)) {
ret = (T[]) val;
}
return ret;
}
public static void main(String[] args) {
Map<String, Object> delegate = new HashMap<>();
int[] intArray = new int[3];
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
delegate.put("intArray", intArray);
Integer[] integerArray = new Integer[3];
integerArray[0] = 1;
integerArray[1] = 2;
integerArray[2] = 3;
delegate.put("integerArray", integerArray);
Test test = new Test(delegate);
//int[] a = test.getValueAsArray("intArray", int[].class);
//System.out.println(Arrays.toString(a));
Integer[] b = test.getValueAsArray("integerArray", Integer[].class);
System.out.println(Arrays.toString(b));
}
}
如果我从上面 class 中取消注释下面的代码,它不会编译。
//int[] a = test.getValueAsArray("intArray", int[].class);
//System.out.println(Arrays.toString(a));
它给出了以下错误:
Error:(44, 31) java: method getValueAsArray in class com.jpmorgan.cu.collections.Test cannot be applied to given types;
required: java.lang.String,java.lang.Class<T[]>
found: java.lang.String,java.lang.Class<int[]>
reason: inferred type does not conform to declared bound(s)
inferred: int
bound(s): java.lang.Object
有人可以解释为什么这不起作用吗?我假设数组是对象,所以 int[] 和 Integer[] 是不同的,但都是对象。
使此方法适用于原始数组和对象数组的最佳方法是什么?
int[]
是引用类型。 Integer[]
是引用类型。 int
是原始类型。 Integer
是引用类型。
T[]
描述了一个数组类型,其中组件类型是泛型类型T
,在某个点被绑定。
如果你打电话给
Integer[] b = test.getValueAsArray("integerArray", Integer[].class);
您正在尝试将 Integer
绑定到 T
。这将起作用,因为 Integer
是一个引用类型,它可以与无界泛型类型变量 T
.
一起使用
但是如果你调用
test.getValueAsArray("integerArray", int[].class);
您正在尝试将 int
绑定到 T
。这将不起作用,因为泛型类型仅适用于引用类型,而 int
是原始类型。
我有以下代码:
public class Test {
private Map<String, Object> delegate;
public Test(Map<String, Object> delegate) {
this.delegate = delegate;
}
public <T> T[] getValueAsArray(String key, Class<T[]> arrayClz) {
T[] ret = null;
Object val = delegate.get(key);
if (val != null && arrayClz != null && arrayClz.isInstance(val)) {
ret = (T[]) val;
}
return ret;
}
public static void main(String[] args) {
Map<String, Object> delegate = new HashMap<>();
int[] intArray = new int[3];
intArray[0] = 1;
intArray[1] = 2;
intArray[2] = 3;
delegate.put("intArray", intArray);
Integer[] integerArray = new Integer[3];
integerArray[0] = 1;
integerArray[1] = 2;
integerArray[2] = 3;
delegate.put("integerArray", integerArray);
Test test = new Test(delegate);
//int[] a = test.getValueAsArray("intArray", int[].class);
//System.out.println(Arrays.toString(a));
Integer[] b = test.getValueAsArray("integerArray", Integer[].class);
System.out.println(Arrays.toString(b));
}
}
如果我从上面 class 中取消注释下面的代码,它不会编译。
//int[] a = test.getValueAsArray("intArray", int[].class);
//System.out.println(Arrays.toString(a));
它给出了以下错误:
Error:(44, 31) java: method getValueAsArray in class com.jpmorgan.cu.collections.Test cannot be applied to given types;
required: java.lang.String,java.lang.Class<T[]>
found: java.lang.String,java.lang.Class<int[]>
reason: inferred type does not conform to declared bound(s)
inferred: int
bound(s): java.lang.Object
有人可以解释为什么这不起作用吗?我假设数组是对象,所以 int[] 和 Integer[] 是不同的,但都是对象。 使此方法适用于原始数组和对象数组的最佳方法是什么?
int[]
是引用类型。 Integer[]
是引用类型。 int
是原始类型。 Integer
是引用类型。
T[]
描述了一个数组类型,其中组件类型是泛型类型T
,在某个点被绑定。
如果你打电话给
Integer[] b = test.getValueAsArray("integerArray", Integer[].class);
您正在尝试将 Integer
绑定到 T
。这将起作用,因为 Integer
是一个引用类型,它可以与无界泛型类型变量 T
.
但是如果你调用
test.getValueAsArray("integerArray", int[].class);
您正在尝试将 int
绑定到 T
。这将不起作用,因为泛型类型仅适用于引用类型,而 int
是原始类型。