Iterator.remove()删除所有元素,而我想删除一个元素

时间:2015-03-11 14:54:50

标签: java arraylist iterator

有关如何从ArrayList中删除特定元素但不是ArrayList的迭代器的问题。使用下面的代码,它会删除ArrayList中的所有元素,但我想删除特定元素,即与播放器冲突的元素。任何人都可以告诉我如何更改此代码,使其这样做?提前谢谢。

public void update() {
        for(Iterator<Object> iter = list.iterator(); iter.hasNext();) {
            Object o = iter.next();
            o.update();
            if(Enemy.collide == true) {
                iter.remove();
            } 
        }  

    }

这是对象类(敌人和玩家类扩展了这个):

    package game;



public abstract class Object {

    protected float x;
    protected float y;
    protected float sX, sY;

    abstract void update();

    public void render() {
        Draw.Rect(x, y, sX, sY);
    }
    public float getX() {
        return x;
    }
    public float getY() {
        return y;
    }
    public float getSX() {
        return sX;
    }
    public float getSY() {
        return sY;
    }
    public float getCenterY() {
        return y + sY/2;
    }
    public void removeObject(Object o) {
        Game.list2.remove(o);
        Game.list = Game.list2;
        Enemy.collide = false;

    }
}

敌人类:

    package game;

import java.util.Iterator;



public class Enemy extends Object {

    private int i = 0;
    private int health = 100;
    private int point = 0;

    static boolean collide = false;

    Enemy(float x, float y) {
        this.x = x;
        this.y = y;
        this.sX = Player.size;
        this.sY = Player.size;
    }


    void update() { 
        if(Physics.checkCollision(this, Game.player)) {
            collide = true;
        }

        if(Math.abs(Game.player.x - this.x) <= 30 && Math.abs(Game.player.x - this.x) >= 1 && Physics.checkCollision(this, Game.player) != true) {
            switch(i) {
            case 1:

                break;
            case 0: 
                if(Game.player.x >= this.x) this.x++;
                if(Game.player.x <= this.x) this.x--;
                if(Game.player.y >= this.y) this.y++;
                if(Game.player.y <= this.y) this.y--;
                break;
            }

        } 




    }


}

玩家类:

package game;

public class Player extends Object {


    public final static  float size = 30;


    public Player(float x, float y) {
        this.x = x;
        this.y = y;
        this.sX = size;
        this.sY = size;
    }
    public void update() {


    }
    public void moveY(double magY) {
        y += magY;
    }
    public void moveX(double magX) {
        x += magX;
    }

}

3 个答案:

答案 0 :(得分:0)

第一个重大问题,请将您的Object课程重命名为GameObject。我没有看到任何import game.Object,因此假设您的EnemyPlayer类来自java.lang.Object

然后,collide应该是一个非静态字段,以允许每个实例的冲突。

for(Iterator<Object> iter = list.iterator(); iter.hasNext();) {
    Object o = iter.next();
    o.update();
    if(o.collide == true) {
        iter.remove();
    } 
}  

此外,您只需使用collide = Physics.checkCollision(this, Game.player if即可。这允许在碰撞结束时在collide上设置false布尔值。

最后,您应该检查Physics.checkCollision(this, Game.player)是否返回预期结果=)

祝你好运!

答案 1 :(得分:0)

我注意到的一些WTF:

  1. public abstract class Object - 认真的?!不要将自己的类命名为java.lang中的类或标准库中的其他类。
  2. static boolean collide = false; ofc你的迭代器遍历你的列表并删除所有这些。只有那些字段应该是静态的,它们与具体实例无关。可能实际问题不同但我强烈建议改变该部分的设计。
  3. 当你处理图形时,
  4. float的准确性很差。它应该导致许多舍入错误。
  5. Physics.checkCollision(this, Game.player)显示此代码,我还建议将此代码移至Object并使用适当的设计模式来处理真正碰撞的对象的不同情况。

答案 2 :(得分:0)

@Nizil是正确的但缺少一小部分才能使其正常工作(我还没有足够的代表发表评论,否则我会这样做。)

如果在敌人类中定义了碰撞,而不是在对象内,则在调用.collide()

之前,首先需要将o投射到敌人身上
for(Iterator<Object> iter = list.iterator(); iter.hasNext();) {
   Object o = iter.next();
   o.update();
   if(o instanceof Enemy){
      Enemy enemy = (Enemy)o;
      if(enemy.collide == true) {
         iter.remove();
      } 
   }
}

您还应该进行非静态碰撞并更改对象的名称(如Nizil建议的那样)

相关问题