• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Generics

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi every body Its Vibhas once again. I am geting a doubt that why does this set of code is not working if i use ArrayList instead of List in the following code i have commented that part by >>>??? Question please have a look on that and expalin me with reason .. Here is the code


[edit]Add code tags, and removed some excess whitespace. CR[/edit]
[ September 19, 2008: Message edited by: Campbell Ritchie ]
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


vibhas karn:

public void calldoctor(List<Animal> animal)//Instead of List if i use
{ //ArrayList its giving me
//compiletime error



If you want this method's parameter to be ArrayList<Animal> then you should call that method passing ArrayList<Animal> to it. But you call that method from



Reference Type is List. So you cannot assign List type in to collection originally declared as ArrayList, reverse is possible.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class AnimalDoctorGenric2
{
public static void main(String[] args)
{

List<Animal> mylist = new ArrayList<Animal>(); mylist is not an ArrayList, but a List. The JVM knows that an ArrayList is a List but not all List's are ArrayLists so you can do List<anyList> yourList = new ArrayList<anArrayList> but you can`t do ArrayList<anArrayList> youArrayList = new List<yourList>...

so if you need your List to came an ArrayList again try that:

ArrayList<Animal> anotherList;

anotherList = (ArrayList)myList;



Dog d = new Dog();
Cat c = new Cat();
Animal a = new Animal();
mylist.add(new Cat("Black"));
mylist.add(new Dog("Doverman"));

for(Object o : mylist)
System.out.println(o);

AnimalDoctorGenric2 ad = new AnimalDoctorGenric2();
//ad.calldoctor(d);

or try it
ad.calldoctor((ArrayList)mylist);
//ad.calldoctor(c);


}
//>>>??? Question
public void calldoctor(List<Animal> animal)//Instead of List if i use
{ //ArrayList its giving me
//compiletime error
//eventhough i am passing Animal
//type reference....??
animal.add(new Dog("Germanshephered"));


}
}

Sometimes programming means
 
Thiago Sanchez
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
:shocked: I forget A DETAIL :shocked:

ad.calldoctor((ArrayList<Animal>) mylist);
//ad.calldoctor(c);


}
//>>>??? Question
public void calldoctor(ArrayList<Animal> animal)//Instead of List if i use
{ //ArrayList its giving me
//compiletime error
//eventhough i am passing Animal
//type reference....??
animal.add(new Dog("Germanshephered"));


}
}
:shocked:
[edit]Disable smilies. CR[/edit]
[ September 19, 2008: Message edited by: Campbell Ritchie ]
 
Marshal
Posts: 80751
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is really difficult to read because you have too much whitespace. That might be caused by your text editor; are you using NotePad? If so, don't, but look for Notepad2 or Notepad++ which are much better tools for programming. They will also help with indentation, matching {} etc.

I can't see what the problem is; I copied and pasted your code and it ran first time. What errors are you actually getting?
The commented-out lines appear to call a method on the AnimalDoctor class which takes Dog or Animal as a parameter, but you have no such method. That might be the cause of your problem, which has nothing whatsoever to do with generics.
 
vibhas karn
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry Ritche i got my mistake .... thanks
 
Campbell Ritchie
Marshal
Posts: 80751
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome ( ) but please tell us how you sorted it out.
 
reply
    Bookmark Topic Watch Topic
  • New Topic