| Author |
dout in genric coll help
|
Chandra shekar M
Ranch Hand
Joined: Dec 20, 2006
Posts: 134
|
|
Hi All, I am using K&B book in that a question is public class test<E>{ public List<E> process(List<E> input){} } the above method SHOULD AND MUST return List<E> only that is it cant even return (implementer)ArrayList<E>.But it can take ArrayList<E> I am confused with this when it is taking (implementer)ArrayList<E> why cant it return the same. But in the same way if I replace List<E> with supercalss and ArrayList<E> with subsclass that is in the same way as above but instead of List<E> and ArrayList<E> a Supercalss<E> and Subsclass<E> (user defined classes) then it will allow to pass the argument and return the argument of type Subclass<E> in the above case ArrayList<E>. But with List<E> and ArrayList<E> it only takes ArrayList<E> but does not Allow to return ArrayList<E>.
|
 |
Anton Uwe
Ranch Hand
Joined: Jan 10, 2007
Posts: 122
|
|
As you can see, the process method is able to return an ArrayList. But you can only refer to it as a List. That way the process-method will still return a List, but now it's not an ArrayList. "public List<E> process(List<E> input)" ensures you can have any List as input (as i.e. ArrayList<E> , but you can only rely on getting back a List<E>. It might be an ArrayList<E>, but it might be a LinkedList<E>,too.
|
 |
Chandra shekar M
Ranch Hand
Joined: Dec 20, 2006
Posts: 134
|
|
Hi Thanks for the reply please see this code when I am passing ArrayList it is Accepting it at Line 1 but in Line 2 I am trying to do the same thing but it throws an compile time error. when it is accepting ArrayList 'al' whose refrence type is also ArrayList then when I am returning a ArrayList 'all' whose refrence type is also ArrayList then why it is throwing error. (edited code tags and formatting) [ January 22, 2007: Message edited by: Barry Gaunt ]
|
 |
Anton Uwe
Ranch Hand
Joined: Jan 10, 2007
Posts: 122
|
|
Bad idea... This has to be
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
What you are attempting to do is to assign an ArrayList<Integer> (which is a List<Integer> ;) to a List<E> which is a list of type what? E is the type of the list you are passing as an argument. That is, E could be anything. The only thing you can return is a List<E> (or a subtype of List<E> ) or a raw List (or subtype of List) with a warning. [ January 22, 2007: Message edited by: Barry Gaunt ]
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Chandra shekar M
Ranch Hand
Joined: Dec 20, 2006
Posts: 134
|
|
Hi I am not able to understand this logic behind this I am guessing that resolution of <E> to <Integer> is creating problem according to you can you please help me in understanding the logic behind this . Thank you very much for the response
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
You are not the only one struggling with this stuff Forget your main method and consider only the method: E is not being resolved to being Integer. E is any type whatsoever. This is a generic class which supposed to work for Integer, String, Apple, Car, Map, anything. Suppose E is chosen to be Apple then you are attempting to return an ArrayList<Integer> instead of an List<Apple>. Suppose E is chosen to be String, then you are attempting to return ArrayList<Integer> instead of an List<String>. Only in the very special case when the E represents Integer would your code be correct. But E has to represent any type for the class to be generic. So you can only return an ArrayList<E> to a List<E> for the code to remain generic. (You can also return a raw type of ArrayList but that gives an unchecked warning).
|
 |
Chandra shekar M
Ranch Hand
Joined: Dec 20, 2006
Posts: 134
|
|
Thank you very much barry gaunt and Anton Uwe in helping me to understand this concept now my dout is cleared basically problem was with <E> type resolution Thanks
|
 |
 |
|
|
subject: dout in genric coll help
|
|
|