Java 向arrayList添加新对象无效
代码运行时没有任何错误,但当它到达layEgg方法时,它会自动添加一个egg,另一个类将bee扩展到Hive中的arrayList。它运行addBee方法并遍历beeList.add,但它仍然没有添加对象。如有任何建议,将不胜感激 我删掉了一些代码,以缩短文章的篇幅。当另一天在Queen中被第三次调用时,运行gJava 向arrayList添加新对象无效,java,arraylist,Java,Arraylist,代码运行时没有任何错误,但当它到达layEgg方法时,它会自动添加一个egg,另一个类将bee扩展到Hive中的arrayList。它运行addBee方法并遍历beeList.add,但它仍然没有添加对象。如有任何建议,将不胜感激 我删掉了一些代码,以缩短文章的篇幅。当另一天在Queen中被第三次调用时,运行g public abstract class Bee { static Hive hive = Garden.hive; protected int type; p
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;
}
}
}
使用断点或调试语句或两者都要检查的内容 所有东西都指向同一个蜂巢对象吗 所有内容都指向同一个列表对象吗 我可以保证在Java中向列表中添加内容是有效的,因此您的代码是: 不加东西 将内容添加到错误的列表中 添加后将其从列表中删除
只需检查所有这些情况并缩小范围,直到找到原因。最有可能的原因是添加了蜜蜂,但您的测试失败了。你能举个例子说明你在做什么,失败的是什么吗?你怎么知道它没有被添加?您是否使用相同的配置单元对象打印蜜蜂?我正在尝试向配置单元中的arrayList添加新对象,该对象是在Queen中创建的,并作为参数传递给Hive,当我使用断点时,我发现它实际上并没有添加新对象。不确定您通过一个示例了解了什么希望答案是itGarden是一个包含Hive的类,但没有对它做任何操作,但它的任何方法都没有被调用。我知道它没有添加,因为它不在添加后的arrayList中。当我调试断点时,你发布了很多不相关的代码,并遗漏了相关内容。蜜蜂和鸡蛋的关系是什么??我建议发布与任何列表相关的所有代码,如果您想将其缩小,则删除不引用任何列表的方法,我可以向您保证,在Java中,向列表中添加内容是不会被破坏的。你要么在不同的列表中添加内容,要么从不同的列表中获取内容,或者在添加项目后将其删除。似乎要将其添加到另一个蜂王在id21中,蛋在id40中似乎很奇怪,因为我不认为我创建了超过1个蜂群列表,因为当蜂群创建为静态蜂群时,这是否意味着问题出在蜂群中的静态蜂群蜂群=Garden.Hive;或者额外的arrayList可能在其他地方?调试的首要规则之一-检查您的假设:我不知道问题出在哪里,因为这是您的代码,对此我看得不够。而且也没有时间免费调试代码。我所能说的就是你设置或传递了错误的东西。每次创建列表时都尝试记录日志,看看发生了什么。例如,您可以执行new Exception.printStackTrace来获取任意代码点的堆栈跟踪。
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();
}
}