Java generics and array initialization

It’s because you can’t create, but you can use them:

public class GenericsTest {
    //statement 1
    public ArrayList<Integer>[] lists;

    public GenericsTest()
    {
        //statement 2
        lists = new ArrayList[4];
        //statement 3
        lists[0].add(new Integer(0));
        //statement 4
        lists[0].add(new String(""));
    }
}

Statement 3 is possible, statement 4 will lead to a compiler error.

Leave a Comment