Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Can an ArrayList store different objects?

Started by Runicode Mar 27, 2011 at 10:37 PM 4 replies 7.5k views
Original Post
Runicode
Runicode
Hi. I have 3 completely different classes, none of which inherit from the same base class. Is there a way I can use an ArrayList to store different objects? If so, can someone provide an example of it's use?

Thanks for your help!
Runicode
EricTheRed
EricTheRed
They do in fact inherit from the same base class. All classes in Java are super classes of the Object class. Just make an array list that can hold Object. You should consider whether or not this is what you really want to do. It sounds like they have no common behaviors, so it may not make much sense to store them in the same array.
Runicode
Runicode

They do in fact inherit from the same base class. All classes in Java are super classes of the Object class. Just make an array list that can hold Object. You should consider whether or not this is what you really want to do. It sounds like they have no common behaviors, so it may not make much sense to store them in the same array.


Oh I see. I didn't realize all objects inherited from Object. They are all semi related objects so I guess I'll go ahead and make a base class for them and just use that as the type for the ArrayList since I do need to restrict what objects can be added. Thank you for your help!
OscarYang
OscarYang
Better yet, simply use Generic Types


ArrayList<Weapons> weaponList = new ArrayList<Weapons>();
//do some work to fill the array
Weapons sword = weaponList.get(5);
//all other object of type Weapons can be retrieved this way without casting
The_Neverending_Loop
The_Neverending_Loop
Your gonna run into problems tho if you just make them all objects, since they are 3 different classes your gonna have to add some extra code so your program will know what to cast the object type to, Everytime I run into something like that, I usually always either find that i have to have 3 different ArrayList for the 3 Different object types, OR make the 3 classes inheret from the same super class, it makes for more organizes code anyhow.
loom_weaver
loom_weaver

[quote name='EricTheRed' timestamp='1301266080' post='4791116']
They do in fact inherit from the same base class. All classes in Java are super classes of the Object class. Just make an array list that can hold Object. You should consider whether or not this is what you really want to do. It sounds like they have no common behaviors, so it may not make much sense to store them in the same array.


Oh I see. I didn't realize all objects inherited from Object. They are all semi related objects so I guess I'll go ahead and make a base class for them and just use that as the type for the ArrayList since I do need to restrict what objects can be added. Thank you for your help!
[/quote]

Also look at using interfaces i.e. composition vs inheritance.

For example, in my game I have Monsters and Items. Mostly different but when it comes to screen display functionality starts to overlap.


interface GlyphEntity {
public String getSymbol();
public Color getColor();
public Point getPosition();
}

class Monster implements GlyphEntity...
class Item implements GlyphEntity...

List<GlyphEntity> itemsToDraw = ...


Then it's easy to iterate over them and you have all the type-safe goodness!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.