ArrayList[] with generics not compiling...

I just can’t seem to create an array of ArrayLists with generics.


private ArrayList<FloorInstance>[] floorBatches;

...

floorBatches = new ArrayList<>[numFloorMeshes];

This gives me this error:

[quote]Incorrect number of arguments for type ArrayList; it cannot be parameterized with arguments <>
[/quote]
So I add the so called missing parameter:

floorBatches = new ArrayList<FloorInstance>[numFloorMeshes];

and I get this error instead:

[quote]Cannot create a generic array of ArrayList<DungeonRenderer3.Chunk.FloorInstance>
[/quote]
This has been nagging me for a while now…

You cannot create an array of generics, because arrays are covariant and generics are (normally) invariant. Combined with the fact that arrays are reified and generics are erased, this makes them incompatible that way – the array can’t store the actual generic type anymore. You can however have a generic parameterized with an array since java can check the cast.

See here: http://docs.oracle.com/javase/tutorial/extra/generics/fineprint.html
More gory details are here: http://www.angelikalanger.com/Articles/Papers/JavaGenerics/ArraysInJavaGenerics.htm

(edit: sorry, posted before my first coffee of the day and I reversed the types you were allowed to create)

You cannot create an array of a generic type, it’s an inherent limitation (or perhaps restriction is a better term) of the compiler. If you do a google search for “array of arraylist” you’ll see lots of people with the same question.

There are a couple of nasty workarounds:

  • use Array.newInstance() and cast the result
  • use ArrayList<ArrayList>( size );

But you should probably consider a redesign that doesn’t require you to ‘mix’ an array of lists.

I must resist…I cannot!! Type erasure sucks.

private ArrayList<FloorInstance>[] floorBatches;

floorBatches = new ArrayList<>[numFloorMeshes];

to


private ArrayList<FloorInstance>[] floorBatches;

floorBatches = new ArrayList<FloorInstance>[numFloorMeshes];

Thanks for the information, guys.

Agreed.

@Pauler
That doesn’t work either.

One argument for scala:

There is the implicit Manifest (before ‘ClassManifest’), the compiler injects it at compile time and it stores type information :wink:

Also, you can create Generic arrays in scala :stuck_out_tongue:

See! :smiley:

I used to agree with you until I saw c++ templates. If you think type erasure is bad, just try to declare a generic variable in the global scope. fyi, you cannot. That sir, is what really sucks here. Give me type erasure any day.

@quew8
Everything’s relative. :stuck_out_tongue:

templates and generics have nothing in common. The first is a macro and the second is a contract.

I just create a non-generic array and cast it to a generic one. Then suppress the warning. :persecutioncomplex:

I reject the theories of why and how this is bad style. I need to get work done.

Excellent quote.

A cute workaround for everyone:


public static <T> T[] newArray(int size, T ... t) {
    return Arrays.copyOf(t, size);
}

...

V[] arrayOfGenerics = newArray(mySize);

ArrayList<MyType>[] myList = newArray(myOtherSize, new ArrayList[]{});

A nice work around. Thanks for sharing.

That convenient method creates an intermediate dummy array.

Whether that’s okay, depends on the use case.

[quote]templates and generics have nothing in common. The first is a macro and the second is a contract
[/quote]
I know they’re not the same thing, they are however often used for equivalent purposes.