简体   繁体   中英

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. 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.

I've tried a getter. Simulation.getCreatures.add(creature) . I've tried accessing it manually. creatures.add(creature) . I've tried making an add method. 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. It creates the creature just fine. But it ignores the addCreature and the print.

(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.

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

As Nexevis and Abhishek Patel already said, you should replace many of the "static" parameters/methods to non-static. 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:

    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:

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.

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. 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.

As @Nexevis said, you seem to be using static incorrectly in the Simulation class. In addition, every function that you need accessible from outside should have a modifier in front of it to set it as public . As per Java documentation,

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.)

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