| Author |
SCJP5 Study Guide Q2. Page 611/622 -- Generics and Collections
|
Ravinder Singh
Ranch Hand
Joined: Mar 06, 2006
Posts: 54
|
|
Dear ranchers, I have a query in the following question that I have customized a bit for testing purpose: Given:- Which statements could be inserted at // INSERT DECLARATION HERE to allow this code to compile and run? (Choose all that apply.) A. List<List<Integer>> table = new List<List<Integer>>(); B. List<List<Integer>> table = new ArrayList<List<Integer>>(); C. List<List<Integer>> table = new ArrayList<ArrayList<Integer>>(); D. List<List, Integer> table = new List<List, Integer>(); E. List<List, Integer> table = new ArrayList<List, Integer>(); F. List<List, Integer> table = new ArrayList<ArrayList, Integer>(); G. None of the above. Correct Answer is B. What if I modify option C a bit to make it look like the following ..and try to recompile and run. But it doesn't compile, why? What is wrong with the type declaration here? List<ArrayList<Integer>> table = new ArrayList<ArrayList<Integer>>(); ------------- Ravinder
|
 |
Vijay Raj
Ranch Hand
Joined: Oct 10, 2005
Posts: 110
|
|
By saying, table.add(row), what you are actually inserting is a List of Integers. Here you cannot add an ArrayList of Integers because you cannot say that a super-class is same as a subclass. So, List<ArrayList<Integer>> table = new ArrayList<ArrayList<Integer>>(); will not work. It'll work if you explicitely cast it to an ArrayList. regards, vijay.
|
 |
 |
|
|
subject: SCJP5 Study Guide Q2. Page 611/622 -- Generics and Collections
|
|
|