Java Generics Syntax for arrays

前端 未结 6 1622
梦谈多话
梦谈多话 2020-12-10 12:17

What data structure does the following declaration specify?

 List[] myArray;

I think it should declare an array where each

6条回答
  •  天涯浪人
    2020-12-10 13:04

    You are correct in saying:

    After running some tests, I determined the declaration means an array where each element is an ArrayList object.

    Executing this code

    List[] myArray  = new ArrayList[2];
    
    myArray[0] = new ArrayList();
    myArray[0].add("test 1");
    
    myArray[1] = new ArrayList();
    myArray[1].add("test 2");
    
    print myArray;
    

    Produces this result:

    {["test 1"], ["test 2"]}
    

    It seems to me there is no reason not to do this instead:

    List myArray  = new ArrayList();
    

提交回复
热议问题