Java question about ArrayList[] x

后端 未结 6 1553
臣服心动
臣服心动 2021-02-06 08:41

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

//declare in class
private ArrayList[] x;

//in constructor
x=new          


        
相关标签:
6条回答
  • 2021-02-06 09:09

    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.

    0 讨论(0)
  • 2021-02-06 09:10

    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 讨论(0)
  • 2021-02-06 09:12

    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.
    
    0 讨论(0)
  • 2021-02-06 09:20

    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>();
    }
    
    0 讨论(0)
  • 2021-02-06 09:25

    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.

    0 讨论(0)
  • 2021-02-06 09:25
    ArrayList<?>[] x;
    x=(ArrayList<? extends Integer>[]) new ArrayList<?>[10];
    x[0] = new ArrayList(1);
    
    0 讨论(0)
提交回复
热议问题