Error: Generic Array Creation

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.

Compilation error: Generic array creation

if you want an array of arraylist:

import java.util.ArrayList;
import java.util.List;

public class Foo{

List [] arrayOfLists = new ArrayList[10];

}

Here is a related post. you cant create a generic array of arraylist.
You can do this:

import java.util.ArrayList;
import java.util.List;

public class Foo{

ArrayList<ArrayList<String>> ll = new ArrayList<ArrayList<String>>();

}

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<?>[]).

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.



Related Topics



Leave a reply



Submit