简体   繁体   中英

Adding new objects to arrayList isnt working

public abstract class Bee {
    static Hive hive = Garden.hive;
    protected int type;
    protected int age;
    protected int health = 3;

    protected int getType()
    {
        return type;
    }
    protected int getAge()
    {
        return age;
    }
    protected int getHealth()
    {
        return health;
    }

    protected void setAge(int age)
    {
        this.age = age;
    }
    protected void setType(int input)
    {
        this.type = input;
    }
    protected abstract boolean eat();
    protected abstract void anotherDay();  //the bees tasks for day (should include eat())

}


    public class Queen extends Bee {

    protected Queen()
    {
        setType(1);
    }
    protected int eggTimer = 0; // tracks when to add a new egg (not using age incase an external factor causes layEgg) 

    // removed code to avoid being too long 

    protected void layEgg() {
        Egg egg = new Egg();
        hive.addBee(egg); //fix??
        eggTimer = 0;
    }
}



        import java.util.ArrayList;

    class Hive {


        ArrayList<Bee> beeList = new ArrayList<Bee>();
        // code removed
        public int beeIndex; // used to know what the index of bee you are in is


        protected void addBee(Bee bee) { //Its running this and getting into the beelist.add(bee) but after there is nothing new in beeList.
            if (beeList.size() < 100) {
                beeList.add(bee);
            } else {
                System.out.println("Too many Bees to add more");
            }
        }

        // removed code to avoid being too long 

        protected void anotherDay() {
            int i = 0;
            for (Bee bee : beeList) {
                i++;
                bee.anotherDay();
                beeIndex = i;
            }
        }
    }

The code runs with no errors but when it gets to layEgg method its ment to add a egg (another class that extends bee) to the arrayList in Hive. It runs and the addBee method and goes over the beeList.add but it isnt adding the object still. Any advice would be greatly appreciated.

I cut out some of the code to make the post shorter layEgg is ran when anotherDay() is called a 3rd time in Queen.

public class Garden {
    static Hive hive = new Hive();
    protected int flowerIndex;
    ArrayList<Flower> flowerList = new ArrayList<Flower>();

    protected void anotherDay() {
        int i = 0;
        for (Flower flower : flowerList) {
            i++;
            flower.anotherDay();
            flowerIndex = i;
        }
    }

    protected void addHive(Hive hiveInput) {
        Hive hive = hiveInput;
    }

    protected void addFlower(Flower flowerInput) {
        Flower flower = flowerInput;
    }

    protected Flower getFlower(int input) {
        return flowerList.get(input);
    }

    protected Flower findFlower() // finds the size of the arrayList and then
                                    // creates a random number within the array
                                    // to use as an index
    {

        int listSize = flowerList.size();
        Random random = new Random();
        int index = random.nextInt(listSize);
        return flowerList.get(index);
    }

    protected int getFlowerListSize()
    {
        return flowerList.size();
    }

}

Things to check (using breakpoints, or debug statements, or both).

  • Is everything pointing to the same Hive object
  • Is everything pointing to the same List object

I can guarantee that adding stuff to Lists in Java works - so your code is either:

  • Not adding the stuff
  • Adding the stuff to the wrong list
  • Removing it from the list after adding

Just check for all those cases and narrow things down until you find the cause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM