• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

***Confusing Generic question ****

 
wonkty wang
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

This is a generic question from k&B book. (p634)

Given a method declared as


A programmer want 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.

I could understand most of the choice except this one:

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

So why this is a incorrect answer? The explaination to this choice is that the return type of process is definitely declared as a List not an ArrayList.

But I thought ArrayList is the subtype of List, so it is equal to say

List<Integer> l = new ArrayList<Integer>();

so what was wrong with it? Can anyone please help?

Thanks.
 
Fu Dong Jia
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi wonkty!
As you said,ArrayList is the subtype of List, so it is equal to say
List<Integer> l = new ArrayList<Integer>();
but is the below correct?
List<Integer> l; ArrayList<Integer> a=l;
Pay more attention to the method return type:List<E>
so if the output is :ArrayList<Integer>output = null;
it will get utput=List<Integer>.
dose it correct?
 
wonkty wang
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh Shit. I must did too many of the questions lately.....How can I overlook it.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic