向arrayList添加新对象不起作用

时间:2013-12-06 12:50:17

标签: java arraylist

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;
            }
        }
    }

代码运行没有错误,但是当它得到layEgg方法时,它将添加一个egg(另一个扩展bee的类)添加到Hive中的arrayList。它运行和addBee方法并遍历beeList.add,但它仍然没有添加对象。任何建议都将不胜感激。

我删除了一些代码以使帖子更短layEgg是在QueenD第三次调用anotherDay()时运行的。

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();
    }

}

1 个答案:

答案 0 :(得分:0)

要检查的事项(使用断点或调试语句,或两者兼而有之)。

  • 是否所有内容都指向同一个Hive对象
  • 是否所有内容都指向同一个List对象

我可以保证在Java中的列表中添加内容 - 所以你的代码是:

  • 不添加内容
  • 将内容添加到错误的列表中
  • 添加
  • 后从列表中删除它

只需检查所有这些情况并缩小范围,直至找到原因。