• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Questions about Generics

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}


}
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like someone else had already posted a similar question here.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
reply
    Bookmark Topic Watch Topic
  • New Topic