Generics en java:certification OCJP
Bonjour,
Toujours dans les generics!, une question de type certif qui je ne comprends pas:
**********************************************
Given a method declared as
Code:
public static <E extends Number> List<E> process(List<E> nums)
A programmer wants to use this method like this
Code:
1 2
| // 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.
Code:
1 2
| ArrayList<Integer> input = null;
ArrayList<Integer> output = null; |
B.
Code:
1 2
| ArrayList<Integer> input = null;
List<Integer> output = null; |
C.
Code:
1 2
| ArrayList<Integer> input = null;
List<Number> output = null; |
D.
Code:
1 2
| List<Number> input = null;
ArrayList<Integer> output = null; |
E.
Code:
1 2
| List<Number> input = null;
List<Number> output = null; |
F.
Code:
1 2
| List<Integer> input = null;
List<Integer> output = null; |
G. None of the above
Réponse:
B, E, and F are correct.
The return type of process is definitely declared as a List, not an ArrayList, so A and D
are wrong. C is wrong because the return type evaluates to List<Integer>, and that can't
be assigned to a variable of type List<Number>. Of course all these would probably cause a
NullPointerException since the variables are still null—but the question only asked us
to get the code to compile. (Objective 6.4)
**********************************************
Pour moi la reponse A est une bonne réponse;Integer est un Number!!
la méthode prend une List<Integer> et donc ArrayList<Integer>(qui est une implementation)
de même pour le type de retour!!
Est c'est une erreur de dans le bouquin?, Merci
Cordialement