JAVA 如何在添加元素 2 秒后从列表中删除元素?

JAVA how to remove element from list 2 seconds after adding it?

我使用的库是处理库。我目前正在制作游戏,我需要在添加项目后每 2 秒从列表中删除项目(项目将通过 Keypressed SPACE 添加)。我试过这个:

List<String> items = new ArrayList<>();

boolean timeStarted;

int timeDuration = 2000;
int startTime = 0;

if (timeStarted) {
            
    int currentDuration = millis() - startTime;

    if (currentDuration >= timeDuration) {

        items.remove(0);

        startTime = millis();
        timeStarted = false;
    }
}

public void keyPressed(KeyEvent e) {

    int keycode = e.getKeyCode();

    if (keycode == 32) { // SPACE
        startTime = millis();
        timeStarted = true;
        items.add("new item"); 
    }

但这只会删除列表中的第一个项目,如果我按 SPACE 太快(同时添加太多项目),它会卡住。

谁能帮助我或告诉我如何处理这个问题?提前致谢!

添加 2 秒后使用 while 检查列表中的元素。

// remove element from list 2 seconds after adding it
while (startedTime+timeDuration >= System.currentTimeMillis()) {
    // If delete exit while
    items.remove(0);
    break;
}

并在您可以按下 space 时使用线程移除您的项目。

if (keycode == 32) { // SPACE
     long startTime = System.currentTimeMillis();
     items.add("new item"); 
     // Thread execute
     Thread thread = new Thread(new Remover(startTime, items));
     thread.start();
}

@Override
public void run() {
    // remove element from list 2 seconds after adding it
    while (startedTime+timeDuration >= System.currentTimeMillis()) {
        // If delete exit while
        items.remove(0);
        break;
    }
     // whether item is deleted
    System.out.println(items);
}

完整代码

public class Main {

    public static void main(String[] args) {
        Main main = new Main();
        
        main.keyPressed(32);

    }
    
    // Integer instead of Keycode
    public void keyPressed(Integer keycode) {
        
        List<String> items = new ArrayList<String>();

        if (keycode == 32) { // SPACE
            long startTime = System.currentTimeMillis();
            items.add("new item"); 
            // Check items
            System.out.println(items);
            
            // Thread execute
            Thread thread = new Thread(new Remover(startTime, items));
            thread.start();
        }
    }
    
}

class Remover implements Runnable{
    
    List<String> items;
    
    int timeDuration = 2000;
    
    long startedTime;
    
    public Remover(long startedTime,List<String> items) {
        this.startedTime = startedTime;
        this.items = items;
    }

    @Override
    public void run() {
        // remove element from list 2 seconds after adding it
        while (startedTime+timeDuration >= System.currentTimeMillis()) {
            // If delete items,Exit while
            items.remove(0);
            break;
        }
         // whether item is deleted
        System.out.println(items);
    }
    
    
}

我个人不会将线程和处理混合搭配在一起。

如果您正在使用 Processing,并且正在为游戏执行此操作(因此可能不需要毫秒精度),为什么不直接使用帧速率作为时间度量?您的处理程序总是应该 运行 一组 frameRate(除非您有一些性能问题)。你也不会看到任何事情发生,直到动作发生的帧被绘制出来,所以如果你的动作在帧之间完成一半并不重要,你的最终用户不会看到差异。

处理中的默认 frameRate 是 30fps。您可以使用 frameRate() 函数 (docs) 设置自定义汇率。 frameCount returns 绘制的帧数(第一帧为 0)。

因此,您可以执行类似于以下伪代码的操作

int removeItemCheckpoint;
int removeItemDuration;

void setup()
{
    // 30 frames per second
    frameRate(30);
    // window is two seconds, so this duration will last two times the frameRate
    removeItemDuration = frameRate * 2;
}

void draw()
{
   if (some input)
   { 
      // set checkpoint to current frame count + the duration
      removeItemCheckpoint = frameCount + removeItemDuration;
   }

   // if current frame count is larger than the checkpoint, perform action
   if (frameCount > removeItemCheckpoint)
   {
      // set checkpoint to pseudo infinity
      removeItemCheckpoint = Integer.MAX_VALUE;
      // remove item here
   }
}

如果您想一次处理多个对象,您可以创建类似于已创建列表 index/checkpoint pairs 的列表,然后循环遍历这些对象。

List<Pair<int, int>> toBeRemoved = new List<Pair<int, int>>();

// add a new pair
toBeRemoved.add(new Pair(itemIndex, frameCount + removeItemDuration));

// loop over the list

foreach (Pair<int, int> item in toBeRemoved)
{
    if (frameCount > item.getKey())
    {
        itemList.remove(item.getValue());
    }
}