This discussion is archived
6 Replies Latest reply: Mar 9, 2008 10:07 AM by 3004 RSS

Two object types in an ArrayList?

807601 Newbie
Currently Being Moderated
Hello!

If I define a class "animals", and one class "food"

Can I create an ArrayList that can hold both animals and food objects?

For instance, if I wanted an ArrayList for food objects, I could write:
ArrayList<Food> global_food = new ArrayList();
My first quess for handing both would be:
ArrayList<Food, Animals> global_food = new ArrayList()
This is not alowed, and I am not sure either wether it is possible to handle multiple objects in an arrayList.

1. Can you handel multiple objects defined in different classes in an arrayList?

2. How would you write to create an arraylist that could handle both?

Regards
Martin
  • 1. Re: Two object types in an ArrayList?
    3004 Newbie
    Currently Being Moderated
    dream77 wrote:
    Hello!

    If I define a class "animals", and one class "food"

    Can I create an ArrayList that can hold both animals and food objects?
    Yes, but it's a bad idea. Why do you want to do this? What are you trying to accomplish?

    >
    For instance, if I wanted an ArrayList for food objects, I could write:
    ArrayList<Food> global_food = new ArrayList();
    My first quess for handing both would be:
    ArrayList<Food, Animals> global_food = new ArrayList()
    No, it'd be <Object>, or of some other common parent class. Or just an unparameterized list.
  • 2. Re: Two object types in an ArrayList?
    807601 Newbie
    Currently Being Moderated
    Curious: why would you want to do this? What purpose would it serve to list incompatable types together?
    My recommendation is as mentioned above: don't do this. This is a very bad idea and will lead to further bad code design. If you tell us the "whys", the what you are trying to achieve, we can probably lead you to a better way to organize your data.
  • 3. Re: Two object types in an ArrayList?
    807601 Newbie
    Currently Being Moderated
    Hello again.

    The example above with animals and food, are just an example. I where coding a small program
    and the ide cross my mind, I wonder if you could store two diffrent types of objects in an arraylist
    I thought for my self. I tryied the first reasonable thing that I could come up with. Unfortenly you could not
    do so, and I got curious if it was possible at all, and how you did it. So for this particular program
    I actually have the animals in one list, and the food in another. :) I just needed some code to paste in
    so that you guys could get the idea.

    so we have cleared out the part, if we can or not, just the second part left, what would you write do do it?

    One part where it should be usefull is if you have one male class and one women class, that shared a couple of things, but
    had diffrent attributs depending what sex they were. Those two could be usefull to store in one arrayList, and you
    could use "instance of" to get the diffrent types printed out. Am I right?

    Regards
    Martin

    Edited by: dream77 on Mar 8, 2008 12:43 PM

    Edited by: dream77 on Mar 8, 2008 12:44 PM
  • 4. Re: Two object types in an ArrayList?
    807601 Newbie
    Currently Being Moderated
    I'd even avoid using instanceOf if possible as over-use of this smells (to me anyway) of bad design. Better is to use inheritance or (even better) interfaces to allow the ArrayList to handle what is given to it without worrying specifically what it is. Perhaps a design pattern might be useful here.
  • 5. Re: Two object types in an ArrayList?
    807601 Newbie
    Currently Being Moderated
    Hello

    Yes it might be a bad design, and there are always better ways to do things. That does not stop my curiosity
    of how you perform a specific task in on way, even if it is a bad one.

    Could someone post a link to an explanation how you do it. I have search, and tryied a few other ways to create that arrayList, but everytime it allways nag about something.

    Like I said, just an example, not going to be used in this program, and I do not know I might newer use it.
    But I am stuborn and want to know anyway.

    Martin
  • 6. Re: Two object types in an ArrayList?
    3004 Newbie
    Currently Being Moderated
    dream77 wrote:
    Hello again.

    The example above with animals and food, are just an example. I where coding a small program
    and the ide cross my mind, I wonder if you could store two diffrent types of objects in an arraylist
    I thought for my self. I tryied the first reasonable thing that I could come up with. Unfortenly you could not
    do so, and I got curious if it was possible at all, and how you did it. So for this particular program
    I actually have the animals in one list, and the food in another.
    If the first element in the Animal list was associate with the first element in the food list, the second with the second, and so on.

    Animals: Lion, Whale, Bird Squirrel
    Food: Antelope, Krill, Worms, Nuts

    Then no, you wouldn't do that. You'd create a class that has both Animal and Food as members, and have a single list of that class.
    One part where it should be usefull is if you have one male class and one women class, that shared a couple of things, but
    had diffrent attributs depending what sex they were. Those two could be usefull to store in one arrayList, and you
    could use "instance of" to get the diffrent types printed out. Am I right?
    Bad idea. Store a common superclass or interface Person and treat all the elments as Person objects, only calling method that are common to all subclasses.

    (As a side note, you probably wouldn't have Man and Woman as seperate classes, just a sex member variable. (Huhuhuh. Hey Beavis, he said "sex member."))