File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions about Generics

 
Engin OKUCU
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stumbled upon a question(in McGraw-Hill/Osborne - Katty Sierra).
Chapter 7 , Questions 16.

I did a copy and paste but it does not compiles and the worst, the given anwser seems not to be correct.
Can you help me ? Thanks.


16. Given a method declared as:
public static <E extends Number> List<? super E> process(List<E> nums)
A programmer wants to use this method like this:
// INSERT DECLARATIONS HERE
output = process(input);
Which pairs of declarations could be placed at // INSERT DECLARATIONS HERE to allow the code to compile? (Choose all that apply.)

A. ArrayList<Integer> input = null;
ArrayList<Integer> output = null;

B. ArrayList<Integer> input = null;
List<Integer> output = null;

C. ArrayList<Integer> input = null;
List<Number> output = null;

D. List<Number> input = null;
ArrayList<Integer> output = null;

E. List<Number> input = null;
List<Number> output = null;

F. List<Integer> input = null;
List<Integer> output = null;

G. None of the above.


Here is what i did :

public class Test {

public static <E extends Number> List<? super E> process(List<E> nums){

return nums;
}

public static void main(String[] args) {
// INSERT DECLARATIONS HERE
ArrayList<Integer> input1 = new ArrayList<Integer>();
ArrayList List<Integer> output = process(input1);

ArrayList<Integer> input2 = new ArrayList<Integer>();
List<? super Number> output2 = process(input2);

ArrayList<Integer> input3 = new ArrayList<Integer>();
List<? super Number> output3 = process(input3);

List<Number> input4 = null;
ArrayList<Integer> output4 = process(input4);

List<Number> input5 = null;
List<Number> output5= process(input5);

List<Integer> input6 = null;
List<Integer> output6 = process(input6);
}


}
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like someone else had already posted a similar question here.
 
ahmed yehia
Ranch Hand
Posts: 424
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check K&B Errata, the correct method signature is:
public static <E extends Number> List<E> process(List<E> nums)
 
Engin OKUCU
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic