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!

3
  • There's a whole bunch of questions and answers on SO about generics and arrays. Commented Mar 11, 2011 at 11:25
  • 2
    why not try List<List<Integer>>? Commented Mar 11, 2011 at 11:26
  • This doesn't exactly answer your question, but you're trying to use an array of ArrayLists, so you are probably better suited to using a LinkedList of Integer arrays. Commented Mar 11, 2011 at 11:39

6 Answers 6

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.
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

0
ArrayList<?>[] x;
x=(ArrayList<? extends Integer>[]) new ArrayList<?>[10];
x[0] = new ArrayList(1);

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

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.