Java 从ArrayList内部删除元素

Java 从ArrayList内部删除元素,java,collections,arraylist,Java,Collections,Arraylist,我有以下代码: class Action { public void step(Game game) { //if some condition met, // then remove self from action stack game.actionStack.remove(this); } class Game ( public ArrayList<Action> actionStack; p

我有以下代码:

class Action {

    public void step(Game game) {
        //if some condition met, 
        // then remove self from action stack
        game.actionStack.remove(this);

}


class Game (

    public ArrayList<Action> actionStack;

    public Game() {
        actionStack = new Arraylist<Action>();
        actionStack.add(new Action());

        while (true) {
            for (Action action : this.actionStack) {
                action.step(this);
            }
        }
    }
}
集体诉讼{
公共无效步骤(游戏){
//如果满足某些条件,
//然后从操作堆栈中删除self
game.actionStack.remove(这个);
}
班级游戏(
公共ArrayList actionStack;
公共游戏(){
actionStack=newArrayList();
添加(新操作());
while(true){
for(操作:this.actionStack){
行动。步骤(本);
}
}
}
}

game.actionStack.remove(this);
发生时会引发异常。是否有方法可以像我所希望的那样从
Action
类中安全地删除元素?

注意:我假设,您为类实现了
equals
hashCode
方法 您需要使用迭代器来删除,如下所示

class Game (

    public ArrayList<Action> actionStack;

    public Game() {
        actionStack = new Arraylist<Action>();
        actionStack.add(new Action());

        while (true) {
            for (Iterator<Action> it = this.actionStack.iterator(); it.hasNext(); ) {
               it.remove();
            }
        }
    }
}
类游戏(
公共ArrayList actionStack;
公共游戏(){
actionStack=newArrayList();
添加(新操作());
while(true){
for(Iterator it=this.actionStack.Iterator();it.hasNext();){
it.remove();
}
}
}
}

编辑:步骤函数正在执行简单的删除操作。我将其移动到
游戏
构造函数

注意:我假设您为类实现了
等于
哈希代码
方法 您需要使用迭代器来删除,如下所示

class Game (

    public ArrayList<Action> actionStack;

    public Game() {
        actionStack = new Arraylist<Action>();
        actionStack.add(new Action());

        while (true) {
            for (Iterator<Action> it = this.actionStack.iterator(); it.hasNext(); ) {
               it.remove();
            }
        }
    }
}
类游戏(
公共ArrayList actionStack;
公共游戏(){
actionStack=newArrayList();
添加(新操作());
while(true){
for(Iterator it=this.actionStack.Iterator();it.hasNext();){
it.remove();
}
}
}
}

编辑:单步函数正在执行简单的删除操作。我将其移动到
游戏
构造函数

我猜您会得到ConcurrentModificationException,因为您在迭代时调用了list remove方法。您不能这样做

一个简单的修复方法是在迭代时处理数组的副本:

for (Action action : new ArrayList<>(this.actionStack)) {
    action.step(this);
}

我猜您得到的是ConcurrentModificationException,因为您在迭代list remove方法时调用了它。您不能这样做

一个简单的修复方法是在迭代时处理数组的副本:

for (Action action : new ArrayList<>(this.actionStack)) {
    action.step(this);
}

我怀疑您遇到了并发修改异常。我建议您这样做

class Action {

    public void step(Game game) {
        //if some condition met, 
        // then remove self from action stack
        List<Action> tmpActionList = new List<Action>();
        tmpActionList = game.actionStack
        tmpActionList.remove(this);
        game.actionStack = tmpActionList;
    }
}
集体诉讼{
公共无效步骤(游戏){
//如果满足某些条件,
//然后从操作堆栈中删除self
List tmpActionList=新列表();
tmpActionList=game.actionStack
tmpActionList.remove(此);
game.actionStack=tmpActionList;
}
}

让我知道它是否有效。

我怀疑您遇到了并发修改异常。我建议您这样做

class Action {

    public void step(Game game) {
        //if some condition met, 
        // then remove self from action stack
        List<Action> tmpActionList = new List<Action>();
        tmpActionList = game.actionStack
        tmpActionList.remove(this);
        game.actionStack = tmpActionList;
    }
}
集体诉讼{
公共无效步骤(游戏){
//如果满足某些条件,
//然后从操作堆栈中删除self
List tmpActionList=新列表();
tmpActionList=game.actionStack
tmpActionList.remove(此);
game.actionStack=tmpActionList;
}
}
让我知道它是否有效。

发件人:我们得到以下信息:

迭代器

请注意,
Iterator.remove
是在迭代过程中修改集合的唯一安全方法;如果在迭代过程中以任何其他方式修改了基础集合,则该行为未指定

当需要执行以下操作时,对每个构造使用
迭代器
,而不是

  • 删除当前元素。每个
构造的
隐藏迭代器,因此无法调用
删除
。因此,每个
构造的
不可用于筛选
  • 并行迭代多个集合
  • 下面的方法演示了如何使用
    迭代器
    过滤任意
    集合
    ——也就是说,遍历集合以删除特定元素

    静态无效过滤器(集合c){
    for(Iterator it=c.Iterator();it.hasNext();)
    如果(!cond(it.next()))
    it.remove();
    }
    
    这段简单的代码是多态的,这意味着无论实现如何,它都适用于任何
    集合

    发件人:我们得到以下信息:

    迭代器

    请注意,
    Iterator.remove
    是在迭代过程中修改集合的唯一安全方法;如果在迭代过程中以任何其他方式修改了基础集合,则该行为未指定

    当需要执行以下操作时,对每个
    构造使用
    迭代器
    ,而不是

    • 删除当前元素。每个
    构造的
    隐藏迭代器,因此无法调用
    删除
    。因此,每个
    构造的
    不可用于筛选
    
  • 并行迭代多个集合
  • 下面的方法演示了如何使用
    迭代器
    过滤任意
    集合
    ——也就是说,遍历集合以删除特定元素

    静态无效过滤器(集合c){
    for(Iterator it=c.Iterator();it.hasNext();)
    如果(!cond(it.next()))
    it.remove();
    }
    
    这段简单的代码是多态的,这意味着无论实现如何,它都适用于任何
    集合


    ConcurrentModificationException如果尝试从