【问题标题】:Iterator.remove() removes all elements while I want to remove oneIterator.remove() 删除所有元素,而我想删除一个
【发布时间】:2015-05-13 10:31:01
【问题描述】:

关于如何从 ArrayList 中删除特定元素而不是 ArrayList 的 Iterator 存在一些问题。使用下面的代码,它删除了 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;
    }

}

【问题讨论】:

  • 不,我们不能,因为我们不知道Enemy.collide 是什么(除了它是一个在您的代码上下文之外设置的 single 布尔值给定)。您需要使Enemy.collide 与列表中的特定条目相关,也许是您调用的将项目传递到或类似的方法。
  • 你确定 Enemy.collide 对其他人来说是假的。它似乎是一个静态字段。
  • 没有Enemy类代码,你的问题毫无意义。
  • 您的 Enemy.collide 属性似乎是静态的并且独立于对象 o
  • 等等...o.update() ? java Object 中没有 update() 函数!请切勿致电个人课程Object。这令人困惑,并可能导致导入问题。

标签: java arraylist iterator


【解决方案1】:

我注意到的一些 WTF:

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

【讨论】:

    【解决方案2】:

    第一个大问题,请将您的 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) 是否返回预期结果=)

    祝你好运!

    【讨论】:

    • @Programmer_10111 嗯,collide 字段属于Enemy,所以你不能用o.collide 访问它,因为o(Game)Object。您可以将此字段移动到(Game)Object,或者更好的是,创建一个扩展(Game)ObjectCollideObject,然后迭代CollideObject 而不是(Game)Object。你也可以做一个instanceof Enemy 并转换它,((Enemy)o).collide,但这通常是一个糟糕设计的证明;)
    • @Programmer_10111 很高兴为您提供帮助;)
    猜你喜欢
    • 1970-01-01
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 2023-03-29
    相关资源
    最近更新 更多