aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP5 Study Guide Q2. Page 611/622 -- Generics and Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "SCJP5 Study Guide Q2. Page 611/622 -- Generics and Collections" Watch "SCJP5 Study Guide Q2. Page 611/622 -- Generics and Collections" New topic
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.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: SCJP5 Study Guide Q2. Page 611/622 -- Generics and Collections
 
Similar Threads
K&B generics doubt
meaning of double angle brackets(<< >>) in Generics?
KB question pg 611
Generic types
S&B 1.5, Chapter 7, Question 2, answer C