简体   繁体   English

为什么我不能在 Java 中创建泛型数组?

[英]Why can't I create generic array in Java?

I had a problem creating some arrays.我在创建一些 arrays 时遇到问题。

Here is the code这是代码

Predicate<Integer>[] endOfLine = {
    curPoint -> curPoint[0]==r2,
    curPoint -> curPoint[0]==c2,
    curPoint -> curPoint[0]==r1,
    curPoint -> curPoint[0]==c1
};
Consumer<Integer[]>[] move = {
    curPoint -> { curPoint[0]++; },
    curPoint -> { curPoint[1]++; },
    curPoint -> { curPoint[0]--; },
    curPoint -> { curPoint[1]--; }            
};

and eclipse (maybe compiler?) said:和 eclipse (也许是编译器?)说:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Cannot create a generic array of Predicate<Integer[]>

I Googled, and found that generic type parameter array can't be made in Java.我google了一下,发现不能在Java中制作泛型类型参数数组。

I read about compile time and runtime, and I thought it may be not my problem.我阅读了有关编译时间和运行时的信息,我认为这可能不是我的问题。

I saw some code which compile compiled:我看到了一些编译编译的代码:

List<Integer>[] arrayOfLists = new ArrayList[2];
ArrayList<Integer[]> lists =new ArrayList<>();
ArrayList<Consumer []> [] lists = new ArrayList[5];
        
Predicate<Integer[]>[] p = new Predicate[5];
p[0] = a -> a[0] == 0;

Predicate<Integer[]>[] pp = { a -> a[0] == 0 }; //cant be compile

and I read oracle doc and found this:我阅读了 oracle 文档并发现了这个:

"Cannot Create Arrays of Parameterized Types" “无法创建参数化类型的 Arrays”

I concluded my shortcut syntax, {} is the problem.我总结了我的快捷语法,{} 是问题所在。 Shortcut syntax create code with parameterized type.快捷语法创建具有参数化类型的代码。 Is this correct?这个对吗?

You can't create a generic array in Java.您不能在 Java 中创建通用数组。 Arrays are reifiable types, and carry their type information at runtime whereas generics are non reifiable, and their type information is erased after the compile time due to erasure. Arrays 是可具体化的类型,在运行时携带其类型信息,而 generics 是不可具体化的,其类型信息在编译后由于擦除而被擦除。 This is due to the implementation of the type system in java and even though this causes some rough edges and corner cases, it eases the evolution of code into generics.这是由于在 java 中实现了类型系统,尽管这会导致一些粗糙的边缘和角落情况,但它简化了代码向 generics 的演变。 Since generic type information is erased, you don't have them at runtime and that's why you can't create a generic array in java.由于泛型类型信息被擦除,您在运行时没有它们,这就是为什么您不能在 java 中创建泛型数组的原因。

There are two solutions.有两种解决方案。 You can either create an Object array and cast it to the generic type you need.您可以创建一个 Object 数组并将其转换为您需要的泛型类型。 This works if you don't return this internal array.如果您不返回此内部数组,则此方法有效。

final T[] arr = (T[]) new Object[n]

or else要不然

final Object[] arr = new Object[]

You can do the cast when you get the items from this array like this当你像这样从这个数组中获取项目时,你可以进行演员表

T itm = (T) arr[1];

However, if you are returning this internal array, you need to create it reflectively, since you need to maintain the proper reifiable type.但是,如果您要返回此内部数组,则需要以反射方式创建它,因为您需要维护正确的可具体化类型。

static <T> T[] createArr(Class<T> clz) {
    return (T[]) Array.newInstance(clz, 5);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM