开发者

Java question about ArrayList<Integer>[] x

开发者 https://www.devze.com 2023-02-16 09:43 出处:网络
I have always had t开发者_C百科his one issue with arrays of ArrayLists. Maybe you can help. //declare in class

I have always had t开发者_C百科his 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!


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.


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.


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


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消