简体   繁体   English

将项目添加到arraylist不会添加吗?

[英]Adding item to arraylist doesnt add it?

So basically I am making a small project for fun. 所以基本上我是在做一个小项目,很有趣。 I've made a Creature object, and in the Simulation class I've made a creatures list. 我创建了一个Creature对象,在Simulation类中,创建了一个生物列表。 The problem is that no matter how I access the list it will not add the item. 问题是,无论我如何访问列表,它都不会添加该项目。 it seems to completely ignore all the code that is written after the adding statement. 它似乎完全忽略了add语句之后编写的所有代码。

I've tried a getter. 我已经尝试过吸气剂。 Simulation.getCreatures.add(creature) . Simulation.getCreatures.add(creature) I've tried accessing it manually. 我尝试过手动访问它。 creatures.add(creature) . creatures.add(creature) I've tried making an add method. 我试图做一个添加方法。 Simulation.addCreature(creature) . Simulation.addCreature(creature) None of these works 这些作品都不是

This is a snippet of the main class. 这是主要课程的摘要。 Yes i have put it in the main method, and the code before that works perfectly fine. 是的,我已经将它放在main方法中,并且在此之前的代码可以正常工作。 It creates the creature just fine. 它创造的生物就很好。 But it ignores the addCreature and the print. 但是它忽略了addCreature和打印。

(sorry but i was unable to get the code format working. It would be greatly appreciated if someone could help me with that too) (对不起,但是我无法使代码格式正常工作。如果有人也可以帮助我,将不胜感激)

Creature creature = new Creature(420, 120, 120, 1);
Simulation.addCreature(creature);
System.out.println("yes");

This is a snippet of my simulation class. 这是我的模拟课程的摘要。 In the addCreature section it doesnt even print. 在addCreature部分中,它甚至不会打印。

private static ArrayList<Creature> creatures = new ArrayList<>();
static void addCreature(Creature creature){
System.out.println("ADDED CREATURE");
creatures.add(creature);
}

There are no error messages. 没有错误消息。 Thanks a lot in advance and I hope you can point out something that i did wrong. 在此先多谢,希望您能指出我做错的事情。

In case you need the entire code: https://github.com/SearchForMe/Simulation 如果您需要完整的代码: https : //github.com/SearchForMe/Simulation

As Nexevis and Abhishek Patel already said, you should replace many of the "static" parameters/methods to non-static. 正如Nexevis和Abhishek Patel所说,应该将许多“静态”参数/方法替换为非静态。 That being said, that is not the reason for your problems with adding a new creature. 话虽如此,这不是您添加新生物时遇到问题的原因。

I downloaded your code from GitHub and added the following print outs to your main class: 我从GitHub下载了您的代码,并将以下打印输出添加到您的主类中:

    System.out.println("1");
    setDebugActive(false);
    System.out.println("2");
    frame = new Frame();
    System.out.println("3");
    Simulation.setSimulationState(true);
    System.out.println("4");
    System.out.println("5");
    Creature creature = new Creature(420, 120, 120, 1);
    System.out.println("6");
    Simulation.addCreature(creature);
    System.out.println("yes");
    System.out.println(Simulation.getCreatures().size());

I noticed that only 1-5 were printed out, and immediately after those print outs came multiple print outs such as: 我注意到只打印了1-5个,在这些打印输出之后,立即出现了多个打印输出,例如:

Found food at: 63 32
New Position: 122 121
moved

From this I found that you are using a while loop inside of the constructor of your Creature class, thus the constructor never returns and the code never reaches the point where it adds to the ArrayList. 由此,我发现您在Creature类的构造函数内部使用了while循环,因此构造函数从不返回,代码也从未到达将其添加到ArrayList的地步。

You should never have an infinite while loop in a constructor... never... Instead, I'd suggest using something such as the Timer class in order to schedule updates. 在构造函数中,永远不要有无限的while循环...永远不要...相反,我建议您使用Timer类之类的东西来安排更新。 I would have the Timer call an update function in Simulation, and then simulation calls an update function in each of the creatures in that function. 我将让Timer在Simulation中调用一个更新函数,然后在该函数中的每个生物中模拟调用一个更新函数。

As @Nexevis said, you seem to be using static incorrectly in the Simulation class. 正如@Nexevis所说,您似乎在Simulation类中错误地使用了static。 In addition, every function that you need accessible from outside should have a modifier in front of it to set it as public . 此外,您需要从外部访问的每个函数都应在其前面加上一个修饰符,以将其设置为public As per Java documentation, 根据Java文档,

If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.) 如果一个类没有修饰符(默认值,也称为package-private),则它仅在其自己的包中可见(包被命名为相关类的组,您将在以后的课程中对其进行了解。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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