8

I have always had this one issue with arrays of ArrayLists. Maybe you can help.

//declare in class
private ArrayList<Integer>[] x;

//in constructor
x=new ArrayList[n];

This generates a warning about unchecked conversion.

But

x=new ArrayList<Integer>[n];

is a compiler error.

Any idea?

Thanks!

adarshr
  • 61,315
  • 23
  • 138
  • 167
David Robles
  • 9,477
  • 8
  • 37
  • 47

6 Answers6

5

I think you cannot make array of generic arraylist because no generic information will be available at runtime.Instead you can do like this:

List<Integer>[] arr=new ArrayList[30];
arr[0]=new ArrayList<Integer>();//create new arraylist for every index.
Emil
  • 13,577
  • 18
  • 69
  • 108
3

You can't make a array of generics lists. Fortunately, there are workarounds. And even more fortunately, there is a nice site about Generics with more information than you'd ever want to know. The link goes straight to the Arrays in Java Generics part.

extraneon
  • 23,575
  • 2
  • 47
  • 51
0

Well, it seems you want to avoid the warning about Generics when you simply write

ArrayList[] x=new ArrayList[n];

as well as remove the compilation error, of x=new ArrayList<Integer>[n];

You can use the generic type <?> to satisfy both the conditions, something like:

ArrayList<?> x[]=new ArrayList[n];

You might want to refer to the link below for knowing more about generics: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

Mehul
  • 47
  • 5
0
ArrayList<?>[] x;
x=(ArrayList<? extends Integer>[]) new ArrayList<?>[10];
x[0] = new ArrayList(1);
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112
0

Run the flowing code:

public class Test {
    ArrayList<Long>[] f0;
    ArrayList<Long> f1;
    ArrayList[] f2;
    public static void main(String[] args) {
        Test t = new Test();
        Field[] fs = t.getClass().getDeclaredFields();
        for(Field f: fs ){
            System.out.println(f.getType().getName());
        }

    }

}

You will get:

[Ljava.util.ArrayList;
java.util.ArrayList
[Ljava.util.ArrayList;

Because Java don't support generic array. When you declare:

private ArrayList<Integer>[] x;

The compiler will think it is :

private ArrayList[] x;

So, you should do like that:

int n = 10;
ArrayList<Long>[] f = new ArrayList[n];
for(int i=0;i<n;i++){
    f[i] = new ArrayList<Long>();
}
gelosie
  • 413
  • 1
  • 3
  • 11
0

It shouldn't have been an error. A warning is enough. If nobody can create an ArrayList<Integer>[], there is really no point to allow the type.

Since javac doesn't do us the favor, we can create the array ourselves:

@SuppressWarnings("unchecked")
<E> E[] newArray(Class<?> classE, int length)
{
    return (E[])java.lang.reflect.Array.newInstance(classE, length);
}

void test()
    ArrayList<Integer>[] x;
    x = newArray(ArrayList.class, 10);

The type constraint isn't perfect, caller should make sure the exact class is passed in. The good news is if a wrong class is passed in, a runtime error occurs immediately when assigning the result to x, so it's fail fast.

irreputable
  • 44,725
  • 9
  • 65
  • 93