每500帧从ArrayList中移除元素 [英] Removing element from ArrayList every 500 frames

查看:69
本文介绍了每500帧从ArrayList中移除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个arraylist:

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

如何构造语句,以便每500帧删除predators数组列表中的最后一个元素?是否需要某种循环?

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

推荐答案

如果您已经有了一个变量,可以跟踪所处的帧,则可以使用以下if语句:

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
}

由于您将1用作ArrayList的remove方法的参数,因此我也这样做了,但是请注意,由于arrayList索引从0开始计数,因此它将始终删除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.

这只会在每次帧计数是500的倍数时运行.

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

如果尚未跟踪frameCount,则必须将frameCount++放入每帧执行的循环中.

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

这篇关于每500帧从ArrayList中移除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆