Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

java - 如何在Java中创建通用数组?(How to create a generic array in Java?)

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

(由于Java泛型的实现,因此不能有以下代码:)

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?

(如何在保持类型安全的同时实现此目的?)

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

(我在Java论坛上看到了这样的解决方案:)

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.

(但是我真的不知道发生了什么。)

  ask by tatsuhirosatou translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(我不得不问一个问题:您的GenSet “选中”还是“未选中”?)

What does that mean?

(这意味着什么?)

  • Checked : strong typing .

    (检查强打字 。)

    GenSet knows explicitly what type of objects it contains (ie 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 .

    (GenSet明确知道其包含的对象类型(即,使用Class<E>参数显式调用了其构造函数,并且当方法传递非E类型的参数时,方法将引发异常。请参见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:

    (请注意,数组的组件类型应该是type参数的擦除 :)

     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]; } ... } 

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中已知的,有意的泛型弱点造成的:它是使用擦除实现的,因此“泛型”类不知道它们在运行时使用什么类型参数创建,因此无法提供类型-安全,除非实施了一些明确的机制(类型检查)。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...