Page 1 of 3 123 LastLast
Results 1 to 20 of 50
  1. #1
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default Retrieving specific objects from ArrayList

    Hello to all,

    I have a little problem. I cant seem to figure out how to retrieve specific objects from ArrayList.
    Lets say that I have class Animal and subclasses Dog and Cat

    In my code I do something like this

    abstract class animal impelemts serializable{
    some code.....
    }
    class dog extends animal{
    some code....
    }
    class cat extends animal{
    some code....
    }


    Main program:

    List<Animal> animals = new ArrayList<Animal>();

    Then i add some dogs and cats to the arraylist

    Dog d1 = new Dog("Jeff", 12);
    Dog d2 = new Dog("Craig", 15);
    Cat c1 = new Cat("Barbie", 2);

    animals.add(d1);
    animals.add(d2);
    animals.add(c1);

    NOW the questions is how I retrieve only dogs from the ArrayList and print them on the screen?

    I tried something like this but it only prints out the last Dog.

    Iterator<Animal> i = animals.iterator();
    while(i.hasnext(){
    if(i.next() instanceof Dog)
    System.out.println(i.next());
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Look what you're doing: you retrieve an animal with i.next() in your if-clause and when it is a dog you retrieve the *next* dog and print it. Only use one i.next() call in the body of your while loop.

    kind regards,

    Jos

  3. #3
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Look what you're doing: you retrieve an animal with i.next() in your if-clause and when it is a dog you retrieve the *next* dog and print it. Only use one i.next() call in the body of your while loop.

    kind regards,

    Jos
    Thank you Jos for reply,

    If I change it to this:

    Iterator<Animal> i = animals.iterator();
    while(i.hasnext(){
    if(i.next() instanceof Dog)
    System.out.println(i);
    }


    The result will print
    java.util.AbstractList$Itr@9304b1
    java.util.AbstractList$Itr@9304b1

    It should print the names and the age but all I get is that.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Don't just guess; you're printing the Iterator itself, not the Animal and that is what an Iterator looks like; better do something like this:

    Java Code:
    while (i.hasNext()) {
       Animal a= i.next();
       if (a instanceof Dog) 
          System.out.println(a);
    }

    or even better: use the new for-loop:

    Java Code:
    for (Animal a : animals)
       if (a instanceof Dog)
          System.out.println(a);

    kind regards,

    Jos

  5. #5
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Don't just guess; you're printing the Iterator itself, not the Animal and that is what an Iterator looks like; better do something like this:

    Java Code:
    while (i.hasNext()) {
       Animal a= i.next();
       if (a instanceof Dog) 
          System.out.println(a);
    }

    or even better: use the new for-loop:

    Java Code:
    for (Animal a : animals)
       if (a instanceof Dog)
          System.out.println(a);

    kind regards,

    Jos
    Oh sweet, now i see it.
    Thanks Jos, saved my day!

  6. #6
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Sorry to bother you guys again but again I have small problem concerning these ArrayLists.
    I am tryin to retrieve specific objects from the ArrayList by the name that user inputs.
    Lets say I want to print out all the dogs that have name Jeff.

    I tried something like this but it just wont print out anything.

    Java Code:
    System.out.println("Give the name of the dog");
    String name = readText(); //another method that brings the text
    for(Animal a : animals){
       String name2 = a.getName(); //method that is in the dog class
      if(a instanceof Dog && name==name2)
         System.out.println(a);
    }
    Any ideas? Help would be appreciated

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,728
    Blog Entries
    7
    Rep Power
    21

    Default

    Don't compare Strings like that; if you had read the API documentation for the String class you would've found something like name.equals(name2).

    kind regards,

    Jos

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    it is better to go for equalsgnoreCase()
    Ramya:cool:

  9. #9
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Oh, of course... Dumbass me.
    Has been such a long time since I have done anything in Java.
    But thanks again Jos and Ramya!
    This is a great forum!

  10. #10
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Oh boy, here we go again. Got stuck again in a part of the program.

    Let say have dogs that have names Jeff,Jeff,Doug,Happy

    When I print out the Dogs I would like it to print only as follows:
    Jeff
    Doug
    Happy

    I dont want it to print out the Jeff name twice although there is two Dog objects in the arrayList named Jeff.

    An ideas? Tried to understand it from Comparable API - documentation and search for examples but no idea.

  11. #11
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You could chuck them into a Set and print the elements of the set instead (assuming you have overridden equals appropriately).

  12. #12
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Do you mean like putting the whole ArrayList to a Set?

    Something like:
    Java Code:
    Set anim = new HashSet(animals); // animals == arraylist
    I dont quite figure it out how I should then compare these two to see if there is the same name in them. Then it would only print it out once.

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    After adding everything into the set you would just need to print out the elements in the set. No need for any more comparisons.

  14. #14
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Ok I tried it this way in a method

    Java Code:
    private void getDogs(){
    		System.out.println("Dogs with different name");
    		Set dogs = new HashSet(animals);
    		Iterator i = dogs.iterator();
    		while(i.hasNext()){
    			Dog d = (Dog) i.next();
    			System.out.println(d.getName());   //method in the Dog class
    		}
    	}
    It still prints out all the dogs, like Jeff twice and so on.....

  15. #15
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You did not see the part of my response above which said

    assuming you have overridden equals appropriately.
    Override the equals method in the Dog class so that it returns true if the dog names are equal.

  16. #16
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Oh well, Still cant figure it out.
    I put the following method in the dog class

    Java Code:
    	public boolean equals(Dog d){
    		return this.name.equalsIgnoreCase(d.name);
    	}
    Do I still have to somehow call/compare it in the code where I print out the names?

  17. #17
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    equalsIgnoreCase() is valid for string types.Could u paste the complete piece of code what u tried with code tag.Just we have a look and ive u suggestion
    Ramya:cool:

  18. #18
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read this article (don't worry it's short) to find out why that's not a very good implementation of the equals method.
    Like I said after the override you won't need to do anymore comparisons. All you will need to do is to print the set.

  19. #19
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Ok, this is what I have done so far.

    The code in the main class:
    Java Code:
    	private void getDogs(){
    		System.out.println("Dogs with different name");
    		Set dogs = new HashSet(animals);
    		Iterator i = dogs.iterator();
    		while(i.hasNext()){
    			Dog d = (Dog) i.next();
    				System.out.println(d.getName());
    		}
    	}
    And here is the Dog class:

    Java Code:
    public class Dog extends Animal{
    	
    some code
    .
    .
    .
    
    	
    	public boolean equals(Object obj){
    		if(this == obj){
    			return true;
    		}
    		if(!(obj instanceof Dog)){
    			return false;
    		}
    		Dog d = (Dog)obj;
    		return name.equalsIgnoreCase(d.getName());
    	}
    	
    	public String getName(){
    		return name;
    	}
    
    }
    Last edited by soketti; 10-26-2009 at 02:56 PM.

  20. #20
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Better call that method printDogs because it doesn't really get any dogs does it?
    So what happens when you run it? Does it work?

Page 1 of 3 123 LastLast

Similar Threads

  1. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 09:51 PM
  2. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:24 AM
  3. Replies: 2
    Last Post: 11-08-2008, 11:40 AM
  4. Retrieving out objects from Vector
    By tuckker in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 06:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •