Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

dout in genric coll help

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>.
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bad idea...
This has to be
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Shiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic