Results 1 to 8 of 8
  1. #1
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default Help with objects in ArrayList

    Hi, can someone please help.
    I have 4 external classes called Cat, Dog etc.
    My test program instantiates them i.e. Cat cat = new Cat();
    Afterwards I placed them in an ArrayList called 'animals':

    animals.add(cat);
    ""
    "'
    ""

    I then created an enhanced 'for' loop:
    The idea is to loop through each object and call its public methods.

    for(Obect obj : animals)
    {
    obj.methodName(); //(The idea is that obj represents 'cat')
    }

    This creates an error. Is it even possible?
    Thanks in advance.
    Sorry, I only speak machine language. Yes or a No?:confused:

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    Yes, if you define an "Animal" base class and have them extend that (or interface and have them implement that) and then use that class/interface instead of "Object".

  3. #3
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    The four external classes in the ArrayList all extend and implement an abstract class called Animal. I cannot create an instance of Animal as it is abstract.
    Sorry, I only speak machine language. Yes or a No?:confused:

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    No, but you can reference one. Try it. Change Object to Animal both in the generics definition of the List, and in the for loop.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,039
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by BiteMuncher View Post
    The four external classes in the ArrayList all extend and implement an abstract class called Animal. I cannot create an instance of Animal as it is abstract.
    Also use a generic List<Animal>; such a list stores animal objects in contrast with a 'raw' List (as you're probably are using now) that stores just Objects.

    kind regards,

    Jos
    I have the stamina of a seal; I lie on the beach instead of running on it.

  6. #6
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    I've now got:

    for(Animal : animals)
    {
    System.out.print("This is: "); Animal.describe();
    }

    Error: incompatible types
    required: Animal
    found: java.lang.Object

    Don't know if I've got this right.
    Sorry, I only speak machine language. Yes or a No?:confused:

  7. #7
    BiteMuncher's Avatar
    BiteMuncher is offline Member
    Join Date
    Feb 2011
    Location
    South Africa
    Posts
    51
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    Also use a generic List<Animal>; such a list stores animal objects in contrast with a 'raw' List (as you're probably are using now) that stores just Objects.

    kind regards,

    Jos
    Thanks, this seems to have solved it!!
    Sorry, I only speak machine language. Yes or a No?:confused:

  8. #8
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Wink

    Quote Originally Posted by BiteMuncher View Post
    for(Animal : animals)
    Does this even work? It should be something like
    Java Code:
    for(Animal [COLOR="Red"]a[/COLOR]: animals)

    Post the whole code if it's not too much lengthy.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. ArrayList duplicated/replaced objects
    By ImAFycus in forum New To Java
    Replies: 3
    Last Post: 05-29-2010, 07:58 PM
  2. Replies: 9
    Last Post: 02-10-2010, 04:19 AM
  3. How can i store ArrayList objects in Access database
    By frankycool in forum Advanced Java
    Replies: 3
    Last Post: 11-04-2009, 07:55 AM
  4. Retrieving specific objects from ArrayList
    By soketti in forum New To Java
    Replies: 49
    Last Post: 10-29-2009, 08:10 AM
  5. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 09:51 PM

Posting Permissions

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