如何在Java中创建一个通用的阵列? [英] How to create a generic array in Java?

查看:731
本文介绍了如何在Java中创建一个通用的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Java泛型的实现,你不能有code是这样的:

Due to the implementation of Java generics, you can't have code like this:

public class GenSet<E> {
    private E a[];

    public GenSet() {
        a = new E[INITIAL_ARRAY_LENGTH]; // error: generic array creation
    }
}

我如何能实现这一点的同时保持类型安全?

How can I implement this while maintaining type safety?

我看到是这样的关于Java论坛的解决方案:

I saw a solution on the Java forums that goes like this:

import java.lang.reflect.Array;

class Stack<T> {
    public Stack(Class<T> clazz, int capacity) {
        array = (T[])Array.newInstance(clazz, capacity);
    }

    private final T[] array;
}

但我真的不明白是怎么回事。谁能帮助?

But I really don't get what's going on. Can anyone help?

推荐答案

我要问的回报一个问题:在你的柴油发电机组检查或取消选中?
这是什么意思?

I have to ask a question in return: is your GenSet "checked" or "unchecked"? What does that mean?


  • 经过强类型的。 柴油发电机组知道明确它所包含的对象类型(即它的构造函数被显式调用了类&LT; E&GT; 参数,当它们被传递,并且是类型电子,见<一不参数的方法将抛出一个异常href=\"http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#checkedCollection%28java.util.Collection,%20java.lang.Class%29\"><$c$c>Collections.checkedCollection.

  • Checked: strong typing. GenSet knows explicitly what type of objects it contains (i.e. its constructor was explicitly called with a Class<E> argument, and methods will throw an exception when they are passed arguments that are not of type E. See Collections.checkedCollection.

- >在这种情况下,你应该写:

-> in that case, you should write:

public class GenSet<E> {

    private E[] a;

    public GenSet(Class<E> c, int s) {
        // Use Array native method to create array
        // of a type only known at run time
        @SuppressWarnings("unchecked")
        final E[] a = (E[]) Array.newInstance(c, s);
        this.a = a;
    }

    E get(int i) {
        return a[i];
    }
}


  • 未选中弱类型的。没有类型检查任何作为参数传递的对象实际上完成的。

  • Unchecked: weak typing. No type checking is actually done on any of the objects passed as argument.

    - >在这种情况下,你应该写

    -> in that case, you should write

    public class GenSet<E> {
    
        private Object[] a;
    
        public GenSet(int s) {
            a = new Object[s];
        }
    
        E get(int i) {
            @SuppressWarnings("unchecked")
            final E e = (E) a[i];
            return e;
        }
    }
    

    请注意,数组的组件类型应该是删除的类型参数的

    Note that the component type of the array should be the erasure of the type parameter:

    public class GenSet<E extends Foo> { // E has an upper bound of Foo
    
        private Foo[] a; // E erases to Foo, so use Foo[]
    
        public GenSet(int s) {
            a = new Foo[s];
        }
    
        ...
    }
    


  • 这一切都从已知的,和蓄意的,在Java泛型的弱点的结果:它使用擦除实现的,所以通用类不知道什么类型的说法,他们在运行时使用创建的,因此不能提供类型安全,除非一些明确的机制(类型检查)被实现。

    All of this results from a known, and deliberate, weakness of generics in Java: it was implemented using erasure, so "generic" classes don't know what type argument they were created with at run time, and therefore can not provide type-safety unless some explicit mechanism (type-checking) is implemented.

    这篇关于如何在Java中创建一个通用的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆