Java Generics Syntax for arrays

前端 未结 6 1625
梦谈多话
梦谈多话 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:00

    List is a List capable of holding ArrayList objects List [] is an array of such Lists

    So, what you said is that An Array of (List of ArrayList object) is CORRECT.

    Can you share what your tests were. My own tests are different

    import java.util.*;
    
    public class TestList {
        public static void main(String ... args) {
            class MySpecialLinkedList extends LinkedList> {
                MySpecialLinkedList() {
    
                }
    
                public void foo() {
    
                }
    
    
                public Object clone()
                {
                    return super.clone();
                }
            }
    
            List> [] someListArray = new MySpecialLinkedList[10];
            for (int i = 0; i < 10; ++i) {
                someListArray[i] = new LinkedList>();
                for (int j = 0; j < 20; ++j) {
                    someListArray[i].add(new ArrayList());
                    for (int k = 0; k < 30; ++k) {
                        someListArray[i].get(j).add(j);
                    }
                }
            }
        }
    }
    

提交回复
热议问题