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

Generics Doubt in for loop

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to create new files Animal class and DOg class which extends Animal and genericTesting Class which makes use of Animal and Dog class.I am facing problem in using new for loop as per Java 1.5


import java.util.ArrayList;
import java.util.List;

public class GenericTesting extends Animal {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List<? super Dog> animalList= (List<? super Dog> new ArrayList<Dog>();

Animal animalObject = new Animal();
animalObject.addAllAnimals(animalList);
}


}


DOg Class

public class Dog extends Animal{

}


Animal Class where i am getting compiler error on for loop

import java.util.List;


public class Animal {

public Animal(){

System.out.println("I am animal ");
}

public void addAllAnimals(List<? super Dog> animalList){
animalList.add(new Dog());


for(List<Animal> d : animalList)
{}



}
}


what should be the syntax for ..FOR loop

how do i iterate through animal list which can be dog or animal as i have used wildcard with super keyword

Thanks in advance

Source:My own
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, "List<? super Dog>" in a function argument list means: This function can accept any list whose generic type is a Dog or higher than Dog or even Object. And, we can ADD a Dog or anything below Dog to "animalList".

And, in the for-each loop, the left hand side of the : is a reference type that points to what type of element is returned by the collection on the right hand side. So, our right hand side, i.e. animalList, can accept Dog or higher than Dog. In accordance with that, your left hand side should be big enough to accept Dog or higher than Dog, which can even be an Object.

If you give for(List<Animal> d : animalList), it can accept only Animal. So, the right thing should be for(Object d : animalList), which can hold anything of the 'animalList' collection(Which can be a Dog, Animal or Object).
 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rekha

can't we write as for(List< ? super Dog> d : animalList)
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mukki,
As I said earlier, the enhanced for loop syntax should be noted.
K&B says this...

declaration: The newly declared block variable, of a type compatible with the elements of the array you are accessing. This variable will be available within the for block, and its value will be the same as the current array element.



So, in for (ElementType variable : Some_collection), the "ElementType" is the type of EACH element in "Some_collection". So, in your said example, we need to put Object in place of "ElementType" so that it can accept anything of the 'animalList' collection.

Hope I am clear.
[ October 20, 2008: Message edited by: Rekha Srinath ]
 
mukki pandey
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Rekha

I got your point.
 
reply
    Bookmark Topic Watch Topic
  • New Topic