1

I have this arraylist:

// Add predators
predators = new ArrayList();
for (int i = 0; i < predNum; i++) {
  Creature predator = new Creature(random(width), random(height), 2);
  predators.add(predator);
}

How can the statement be structured so that the last element from the predators arraylist is removed every 500 frames? Does it need a loop of some sort?

if (frameCount == 500){
 predators.remove(1)
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
Jonathan Laliberte
  • 2,672
  • 4
  • 19
  • 44
  • you want remove 1 item from the list or everything in the arraylist is removed? – logger Jan 20 '16 at 18:25
  • 1
    What is a frame? What is every 500 frames? Never heard of that term... – randers Jan 20 '16 at 18:25
  • @user3659052 just the last item in the arraylist each time the program does 500 frames. – Jonathan Laliberte Jan 20 '16 at 18:26
  • @RAnders00 in Processing 3.0.1, you can have a count of each frame with 'frameCount'. – Jonathan Laliberte Jan 20 '16 at 18:27
  • Oh, now I understand, you probably should have included that you are talking about _video_ frames! – randers Jan 20 '16 at 18:29
  • @RAnders00 Not exactly. This question is about the Processing language, which gives you a `draw()` function that's automatically called 60 times per second. Each call to `draw()` is considered a **frame**, and you can draw stuff to the screen, making it easy to create interactive visualizations without all the boilerplate of Java. – Kevin Workman Jan 20 '16 at 18:50

2 Answers2

5

If you already have a variable that keeps track of what frame you are on, you can use this if statement:

if (frameCount % 500 == 0) {
   predators.remove(1); //use this if you want to remove whatever is at index 1 every 500 frames
   predators.remove(predators.size() -1); //use this if you want to remove the last item in the ArrayList
}

Since you used 1 as the argument for the remove method of the ArrayList, I did too, but note that this will always remove the 2nd object in the arrayList since arrayList indices start counting at 0.

This will only run every time the framecount is a multiple of 500.

If you do not already keep track of the frameCount you will have to put frameCount++ in the loop that is executed every frame.

The Coding Wombat
  • 805
  • 1
  • 10
  • 29
  • Awesome, that's pretty much exactly what I was looking for. Thank you very much. Any ideas though why it might be removing half of the elements in the arraylist and not just 1 element ? – Jonathan Laliberte Jan 20 '16 at 18:31
  • 1
    What do you mean by half of the elements? If you only include the second predators.remove line it should only remove the last element. So every 500 frames one predator should be removed from the list. Is this not what is happening? – The Coding Wombat Jan 20 '16 at 18:33
  • Nah. There must be a bug somewhere then Both of the methods you suggested was tried. When i do the second method, i get an output of: 5 2 1 0 (The number of elements left, each iteration) – Jonathan Laliberte Jan 20 '16 at 18:38
  • 1
    Do you put the print statement to see the size of the list right after the remove() call? that way you can see if the "bug" is in the remove() method or in the number of times the if statement evaluates true. – The Coding Wombat Jan 20 '16 at 18:44
  • 1
    @JonathanLaliberte It sounds like you have this if statement inside of a for loop traversing the whole array, in which case it will remove an element, go forward an element, remove an element, go forward an element... and so on until the end. This will result in the behavior of removing half of the array. To fix this issue, don't put the if statement to remove things inside of a for loop – phflack Jan 20 '16 at 18:44
  • 1
    @JonathanLaliberte This could well be the problem, to clarify, this is because the for loop is executed almost all at once every frame, so if every frame the for loop is looped, and inside of the for loop is the if statement I provided above, the frameCount will be the same for the entire for loop, thus resulting in multiple removals like phflack pointed out. – The Coding Wombat Jan 20 '16 at 18:48
  • 1
    Can't express enough how appreciative I am for the great set of comments here. Picking up a ton of useful info. Kudos. Thanks all for putting in some time here to help someone out! Got the thing working now finally :D – Jonathan Laliberte Jan 20 '16 at 19:11
2

The draw() function is called 60 times per second, so that's the loop you would be using. The frameCount variable is incremented automatically each time draw() is called.

Like The Coding Wombat said, you can use the modulo operator to determine when a variable (like frameCount) is a multiple of a value (like 500).

You can combine those ideas to do something once ever 500 frames:

ArrayList<Creature> predators = new ArrayList<Creature>();

void setup(){
  for (int i = 0; i < predNum; i++) {
    Creature predator = new Creature(random(width), random(height), 2);
    predators.add(predator);
  }
}

void draw(){
  if (frameCount % 500 == 0){
   predators.remove(predators.size()-1);
  }

  //draw your frame
}
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • Doesn't remove(0) remove the first index of the arraylist instead of the last like the asker asked for? – The Coding Wombat Jan 20 '16 at 18:42
  • 1
    @TheCodingWombat Good catch. Edited. Btw your answer is correct, but I added my answer to be more specific to Processing. – Kevin Workman Jan 20 '16 at 18:43
  • I only now realize the tag processing is actually the name of a programming language/framework instead of him meaning that he was processing the arraylist – The Coding Wombat Jan 20 '16 at 18:50
  • 1
    @TheCodingWombat Yeah, it's a pretty confusing name for a language. It doesn't help that most Processing questions are also tagged with Java! – Kevin Workman Jan 20 '16 at 18:53