This question already has an answer here:
这个问题在这里已有答案:
- Is it possible to use a primitive type (int) in as a generic type in Java? 2 answers
是否可以在Java中使用基本类型(int)作为泛型类型? 2个答案
EDIT: This question specifically regards Java primitive arrays. There are other questions that address writing generic methods for primitive scalar types, but arrays are sufficiently different to warrant keeping this question around for future reference (IMHO).
编辑:这个问题特别关注Java原始数组。还有其他问题涉及为原始标量类型编写通用方法,但是数组是完全不同的,以保证将此问题保留以供将来参考(IMHO)。
Consider the following Java method:
请考虑以下Java方法:
private int maxIndex(double[] values, double[] maxOut)
{
int index = 0;
double max = values[index];
for (int i = 1; i < values.length; i++) {
if (max < values[i]) {
max = values[i];
index = i;
}
}
if (maxOut != null) {
maxOut[0] = max;
}
return index;
}
Is there a way to write a generic version of this that will work with any of the primitive numeric types? As an example I tried the following:
有没有办法编写一个适用于任何原始数字类型的泛型版本?作为一个例子,我尝试了以下内容:
private <T extends Comparable<T>> int maxIndexDoesNotWork(T[] values, T[] maxOut)
{
int index = 0;
T max = values[index];
for (int i = 1; i < values.length; i++) {
if (max.compareTo(values[i]) < 0) {
max = values[i];
index = i;
}
}
if (maxOut != null) {
maxOut[0] = max;
}
return index;
}
It doesn't work-- presumably because auto-boxing doesn't happen on arrays of primitive types. Not that I really want boxing, of course. What I'd really like is to be able to tell the compiler/runtime that T supports the '<' operator. Is there a way?
它不起作用 - 大概是因为自动装箱不会发生在原始类型的数组上。当然,并不是说我真的想要拳击。我真正喜欢的是能够告诉编译器/运行时T支持'<'运算符。有办法吗?
3 个解决方案
#1
2
There is no way, because unlike autoboxing of individual values, there is no automatic conversion of collections/arrays of primitive element types to their wrapper object equivalent element types.
没有办法,因为与单个值的自动装箱不同,没有将原始元素类型的集合/数组自动转换为它们的包装器对象等效元素类型。
#2
0
No:(
But you can code-generate:)
但你可以代码生成:)
#3
0
Using reflection, you can write a method that will work with primitive arrays or reference arrays, but you give up type safety in the process:
使用反射,您可以编写一个可以使用原始数组或引用数组的方法,但是您在此过程中放弃了类型安全性:
import java.lang.reflect.Array;
private int maxIndex(Object values, Object maxOut)
{
int index = 0;
Object max = Array.get(values, index);
for (int i = 1; i < Array.getLength(values); i++) {
if (((Comparable)max).compareTo(Array.get(values, i)) < 0) {
max = Array.get(values, i);
index = i;
}
}
if (maxOut != null) {
Array.set(maxOut, 0, max);
}
return index;
}
#1
2
There is no way, because unlike autoboxing of individual values, there is no automatic conversion of collections/arrays of primitive element types to their wrapper object equivalent element types.
没有办法,因为与单个值的自动装箱不同,没有将原始元素类型的集合/数组自动转换为它们的包装器对象等效元素类型。
#2
0
No:(
But you can code-generate:)
但你可以代码生成:)
#3
0
Using reflection, you can write a method that will work with primitive arrays or reference arrays, but you give up type safety in the process:
使用反射,您可以编写一个可以使用原始数组或引用数组的方法,但是您在此过程中放弃了类型安全性:
import java.lang.reflect.Array;
private int maxIndex(Object values, Object maxOut)
{
int index = 0;
Object max = Array.get(values, index);
for (int i = 1; i < Array.getLength(values); i++) {
if (((Comparable)max).compareTo(Array.get(values, i)) < 0) {
max = Array.get(values, i);
index = i;
}
}
if (maxOut != null) {
Array.set(maxOut, 0, max);
}
return index;
}