0

Everytime a box, from the arraylist shapes, reaches a specified position, it has to be removed from the arraylist. But it doesn't seems to work, what's im doing wrong?

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

2 に答える 2

2

List 型は、オブジェクト自体をパラメーターとして受け取る ".remove()" のバリアントをサポートしているため、最初にそのインデックスを見つけてからインデックスで削除する必要はありません。そうは言っても、この操作は ArrayList 型では比較的コストがかかります。任意の場所で挿入/削除する場合は、LinkedList を使用します。

于 2012-12-12T08:44:21.643 に答える
1
//data = your Array list
for (Iterator i = data.iterator(); i.hasNext(); ) {
    Object element = i.next();

    if ((..your conition..)) {
       i.remove();
    }
}

各オブジェクトの状態を確認したい場合は、上記のコードを使用してください。しかし、単に呼び出すよりも削除したいオブジェクトのみを持っている場合。

yourList.remove(your-object);

これを見てください。

于 2012-12-12T08:48:46.113 に答える