8

I was trying create an array of a collection as follows.

ArrayList<Integer> ar[]=new ArrayList<Integer>[50];

but it gives me an error -> generic array creation can anybody explain me why is it?

davioooh
  • 23,742
  • 39
  • 159
  • 250
sumit sharma
  • 1,067
  • 8
  • 24

6 Answers6

5

You can't create arrays of generic types. Use collection of collections instead:

ArrayList<ArrayList<Integer>> = new ArrayList<ArrayList<Integer>>();

Why can't we create an array of generic type? Array stores they exact type internally, but due to the type erasure at runtime there will be no generic type. So, to prevent you from been fooled by this (see example below) you can't create an array of generic type:

//THIS CODE WILL NOT COMPILE
ArrayList<Integer>[] arr = new ArrayList<Integer>[5];
Object[] obj = arr;
obj[0] = new ArrayList<Long>(); //no one is safe
Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
1

ArrayList is internally a 1D array itself. what you need 2D array so you can create

ArrayList<Integer[]> ar=new ArrayList<Integer[]>();

or

ArrayList<ArrayList<Integer>> = new ArrayList<ArrayList<Integer>>();
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
1

The answer to you question can be found in the Java Language Specification. You are trying to create an array using Array Creation Expression. It says the following: "It is a compile-time error if the ClassOrInterfaceType does not denote a reifiable type". Because arrays in Java are created at runtime, their type information should be completely available at runtime. In other words array element type should be a reifiable type. Generics in Java are implemented using type erasure (i.e. only subset of generics compile-time type information is available at runtime) hence they are not reifiable by definition and therefore you cannot create arrays of generic types.

Andrii Polunin
  • 1,306
  • 11
  • 15
1

Actually you can have an Array of Collection, it just is not allowed that the Collection has a specific type.
You can do something like

    ArrayList<?>[] ar = new ArrayList<?>[50];  
    // or ArrayList[] ar = new ArrayList[50];
    ar[0] = new ArrayList<Integer>();

but you will not have the benefits of Generics - there is no type information for the content of the Collection, you will need to cast when reading from it

    Integer i = (Integer) ar[0].get(0);
user85421
  • 28,957
  • 10
  • 64
  • 87
  • not sure where such a construct would be better than using a Collection of Collection or eventually an Array of Array (2D Array) – user85421 Jan 08 '13 at 09:34
0

You could do something like this

     ArrayList<Integer> ar[]= new ArrayList[50];

     ArrayList<Integer> intArr  = new ArrayList<Integer>();
     ArrayList<Long> longArr = new ArrayList<Long>();

     ar[0]=intArr;
     ar[1]= longArr; //  compile error Type mismatch: cannot convert from ArrayList<Long> to ArrayList<Integer>
someone
  • 6,577
  • 7
  • 37
  • 60
0

You can have an array "technically" of type ArrayList but its a bit nit picky. Create it as an ArrayList<ArrayList<Integer>> list = ArrayList<ArrayList<Integer>(); and convert it using toArray(ArrayList<Integer>[list.size()]);.

Example:

    ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();

    list.add(new ArrayList<Integer>());
    list.add(new ArrayList<Integer>());
    list.add(new ArrayList<Integer>());

    int count = 1;

    for(ArrayList<Integer> AList: list) {
        for(int i = 0; i < 4; i++)
            AList.add(count++);
    }

    ArrayList<Integer>[] check = list.toArray(new ArrayList[list.size()]);

    for(ArrayList<Integer> AL : check) {
        for(Integer i:AL) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

Output:

 1 2 3 4 
 5 6 7 8 
 9 10 11 12 

Works and is an ArrayList array

Skepi
  • 478
  • 3
  • 12
  • ArrayList>(); hmm ? Also answer with java code standards – vels4j Jan 07 '13 at 09:33
  • It's just ArrayList>(). I didn't say it was good to do. I just said that that's how I would do it... I wouldn't. But that's how I would =-p – Skepi Jan 07 '13 at 09:56