Generic Array Creation Error

Generic array creation error

You can't have arrays of generic classes. Java simply doesn't support it.

You should consider using a collection instead of an array. For instance,

public static ArrayList<List<MyObject>> a = new ArrayList<List<MyObject>();

Another "workaround" is to create an auxilliary class like this

class MyObjectArrayList extends ArrayList<MyObject> { }

and then create an array of MyObjectArrayList.


Here is a good article on why this is not allowed in the language. The article gives the following example of what could happen if it was allowed:

List<String>[] lsa = new List<String>[10]; // illegal
Object[] oa = lsa; // OK because List<String> is a subtype of Object
List<Integer> li = new ArrayList<Integer>();
li.add(new Integer(3));
oa[0] = li;
String s = lsa[0].get(0);

Error generic array creation

You can't create arrays with generics. Use a Collection<Set<Hexagon>> or (Array)List<Set<Hexagon>> instead.

Here's the formal explanation.

Effective Java: What is exactly generics array creation warning

The warning you're referring to is due to the automated array creation that happens in a varargs method, one like this:

public void method(List<String>... listArgs) { ... }

If you call:

method(new ArrayList<String>(), new ArrayList<String>());

You will see the warning (the actual warning text varies according to Java versions, I think):

[unchecked] unchecked generic array creation for varargs parameter of type List<String>[]

What Java does with varargs methods is to create an array of the variable argument parameter, so in this case it will try to create a List<String>[], which is a generic array and thus the warning message (because it will be declassed to List<?>[]).

Generic array creation error on ArrayList

You can only create raw array types. You need to do this: a = new ArrayList[2];

How to create a generic array in Java?

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

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

    ...
    }

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: Generic array creation error?

subTest is an inner class of test, so the type parameter of test is visible from within subTest. Therefore, you are trying to create an array of generic type

test<T>.subTest[]

You can solve this by making the class subTest a static nested class instead:

private static class subTest

If you do this, the type of the array is just

test.subTest[]


Related Topics



Leave a reply



Submit