gpt4 book ai didi

Java Arraylist 删除对象

转载 作者:行者123 更新时间:2023-12-02 00:06:10 24 4
gpt4 key购买 nike

每次数组列表形状中的盒子到达指定位置时,都必须将其从数组列表中删除。但它似乎不起作用,我做错了什么?

if(!box.removing && box.y == MoveY - 50 && MoveX != box.x) {
box.removing = true;
score += 10;

System.out.println(shapes.indexOf(box));

shapes.remove(shapes.indexOf(box));


} else {
// Gravity loop, if not reached the position. This work.
box.update(1);
}

最佳答案

List 类型支持“.remove()”的变体,该变体将对象本身作为参数,因此您不需要先找到其索引,然后按索引删除。话虽这么说,这个操作对于 ArrayList 类型来说是相对昂贵的;如果要在任意位置插入/删除,请使用 LinkedList。

关于Java Arraylist 删除对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13835799/

24 4 0