68

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

skaffman
404k96 gold badges824 silver badges775 bronze badges
asked Aug 20, 2011 at 12:08
2
  • 4
    public static ArrayList<MyObject>[] a = new ArrayList[10]; Commented Mar 16, 2014 at 0:58
  • 12
    This is NOT a duplication! That question is about "new E[n]" while this one is "new Generic<E>[n]". Totally different! Commented Sep 7, 2016 at 17:28

5 Answers 5

77

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 (now archived) 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);
Anerdw
2,6983 gold badges18 silver badges44 bronze badges
answered Aug 20, 2011 at 12:11
5
  • If it would compile you would be able to create inconsistent collections. (Arrays doesn't work like this, and instead rely on throwing an ArrayStoreException at runtime.) I'll dig up a link for you about it... Commented Aug 20, 2011 at 12:16
  • 1
    Here's an article. ibm.com/developerworks/java/library/j-jtp01255/index.html Commented Aug 20, 2011 at 12:18
  • If you try to convert the example from the article so arrays are ArrayList then you get an error here: List<Object> oa = lsa; // Type mismatch: cannot convert from List<List<String>> to List<Object> Why don't allow generics arrays doing the same checks? You could activate it with an annotation losing some backward compatibility in that method. Commented Aug 20, 2011 at 14:04
  • 11
    The same problem here without generics ` String[] x = new String[10]; Object[] y = x; y[1] = 10; //boxing String z = x[1];` Commented Aug 20, 2011 at 14:09
  • 3
    I can't see any difference with this: Integer [] integerArray=new Integer[20]; Object [] objectArray = integerArray; objectArray[0]= "Hello world!"; integerArray[0].floatValue(); This code compiles and at runtime throws this Exception: java.lang.ArrayStoreException: java.lang.String Why? Commented May 1, 2020 at 6:34
11

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>();
}
answered Apr 12, 2014 at 18:27
2
  • 3
    or, to avoid raw types, ArrayList<myObject>[] a = (ArrayList<myObject>[])new ArrayList<?>[2]; Commented Apr 14, 2014 at 5:48
  • Using the raw type ArrayList like this will harm type safety. For example, you could write a[0] = new ArrayList<unrelatedObject>(); and it would compile just fine. Anyway, the question does not concern the contents of the array, so I don't see initialization as relevant. Commented Jul 7, 2020 at 0:48
6

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

answered Aug 20, 2011 at 15:04
0
-1

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...

adarshr
62.8k24 gold badges146 silver badges175 bronze badges
answered Aug 20, 2011 at 12:34
-1

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.

answered Aug 20, 2011 at 20:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.