The title is wordy and possibly confusing but I am not sure how to make it better...I want to be able to access values in my array list and print them out.
I have an interface called ThingBagInterface. This ThingBagInterface only has one method, and looks like this:
interface ThingBagInterface {
public String getType();
}
I now have a class called ThingBag, it's a bag that hold a bunch of different stuff, such as Creatures, Buildings etc.
In my ThingBag class, I have initialized all of my Creatures like this:
public void initCreatures(){
waterSnake = new Creature("Water Snake", Terrain.SWAMP, false, false, false, false, 1, 0);
etc...
}
and then I have a function populateBag() that looks like this:
public void populateBag(){
initCreatures();
bag.add(bears);
}
My array list definition is in ThingBag and looks like this:
ArrayList<ThingBagInterface> bag = new ArrayList<ThingBagInterface>();
My Creature constructor looks like this:
public Creature(String n, Terrain startTerrain, boolean flying, boolean magic, boolean charge, boolean ranged, int combat, int o){
name = n;
flyingCreature = flying;
magicCreature = magic;
canCharge = charge;
rangedCombat = ranged;
combatValue = combat;
owned = o;
}
I want to print out the name of the bear.
So in main I am doing this:
ThingBag tb = new ThingBag();
tb.populateBag();
for(int i= 0; i<tb.bag.size(); i++){
System.out.println(i+". "+tb.bag.get(i));
}
Why can I not access the name in my bag? If I wasn't using an interface I would be able to say:
System.out.println(i+". "+tb.bag.get(i).name)
But I can't now. Any ideas on how I can access that value? I can only access memory addresses now...