Generic array creation error

JavaArraysGenerics

Java Problem Overview


I am trying do something like this:-

public static ArrayList<myObject>[] a = new ArrayList<myObject>[2];

myObject is a class. I am getting this error:- Generic array creation (arrow is pointing to new.)

Java Solutions


Solution 1 - Java

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); 

Solution 2 - Java

There is a easier way to create generic arrays than using List.

First, let

public static ArrayList<myObject>[] a = new ArrayList[2];

Then initialize

for(int i = 0; i < a.length; i++) {
     a[i] = new ArrayList<myObject>();
}

Solution 3 - Java

You can do

public static ArrayList<myObject>[] a = (ArrayList<myObject>[])new ArrayList<?>[2];

or

public static ArrayList<myObject>[] a = (ArrayList<myObject>[])new ArrayList[2];

(The former is probably better.) Both will cause unchecked warnings, which you can pretty much ignore or suppress by using: @SuppressWarnings("unchecked")

Solution 4 - Java

if you are trying to declare an arraylist of your generic class you can try:

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

this will give you an arraylist of myobject (size 10), or if u only need an arraylist of size 2 you can do:

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

or you may be trying to make an arraylist of arraylists:

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

although im not sure if the last this i said is correct...

Solution 5 - Java

It seems to me that you use the wrong type of parenthesis. The reason why you can't define an array of generic is type erasure.

Plus, declaration of you variable "a" is fragile, it should look this way:

List<myObject>[] a;

Do not use a concrete class when you can use an interface.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHarshveer SinghView Question on Stackoverflow
Solution 1 - JavaaioobeView Answer on Stackoverflow
Solution 2 - JavaStrinView Answer on Stackoverflow
Solution 3 - JavanewacctView Answer on Stackoverflow
Solution 4 - JavatemelmView Answer on Stackoverflow
Solution 5 - JavaRostislav MatlView Answer on Stackoverflow