Java generics and array initialization

后端 未结 4 1283
谎友^
谎友^ 2020-12-01 12:41

What\'s the explanation for the following:

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

    public GenericsTes         


        
相关标签:
4条回答
  • 2020-12-01 13:18

    There seems to be obscure cases where you could inadvertently cause a ClassCastException as explained here http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf (section 7.3)

    an intersting discussion on this topic could be found here http://courses.csail.mit.edu/6.170/old-www/2006-Spring/forum/index.php%3Ftopic=324.msg1131.html

    0 讨论(0)
  • 2020-12-01 13:23

    In this case, I would avoid using arrays for just this reason. The declaration of "lists" in your original code could be

    List<List<Integer>> lists = new ArrayList<List<Integer>>(4);
    for(int i = 0; i < 4; i++) lists.add(null); // or add an empty ArrayList<Integer>
    

    (you should use the interface rather than the implementation in variable declarations)

    Instead of array [] syntax, you would use get() or set(). Other than that, it's equivalent.

    0 讨论(0)
  • 2020-12-01 13:25

    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.

    0 讨论(0)
  • 2020-12-01 13:27

    So the actual question is: Why is there no error for declaring a generic array? ?

    You will always get an error at the point you do something erroneous. Adding an error where there isn't technically a problem just adds to a clutter (although an editor might want to point that out to you).

    In some circumstances you may want to bend the rules a bit with an unchecked cast. There's no need to force the code to be littered with more warning suppressions than necessary (other than to point out the folly).

    0 讨论(0)
提交回复
热议问题