简体   繁体   English

如何在迭代时从arraylist中删除一个元素

[英]How to remove just one element from arraylist while iterating over it

I wanted to remove single element which matches orderID = 542 .But the thing is this program is removing two elements from that list.In the real program im iterating through an arraylist and i call a function to check whether that element is to be removed from the list and that function is suppose to remove the element from the list 我想删除与orderID = 542匹配的单个元素。但是事实是这个程序正在从该列表中删除两个元素。在实际程序中我迭代通过一个arraylist并且我调用一个函数来检查是否要从该元素中删除该元素列表和该函数假设从列表中删除元素

package testMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class TestHashMap {
    static ArrayList<LimitOrder> a = new ArrayList<>();

    public static void main(String args[]) {
        create();
    }

    public static void create() {
        LimitOrder l1 = new LimitOrder(1, 100, "145");

        LimitOrder l2 = new LimitOrder(1, 100, "542");

        LimitOrder l3 = new LimitOrder(1, 100, "355");

        a.add(0, l1);
        a.add(1, l2);
        a.add(2, l3);

        Iterator<LimitOrder> i = a.iterator();
        while (i.hasNext()) {
            boolean toremove = false;
            LimitOrder l = i.next();
            toremove=remove();
            if (toremove == true)
            {
                System.out.println("Removed "+l.orderID);
                i.remove();
            }
        }

    }

    public static boolean remove() {
        boolean flag = false;
        Iterator<LimitOrder> i = a.iterator();
        while (i.hasNext()) {
            LimitOrder l = i.next();
            if (l.orderID.equals("542")) {
                flag = true;
            }
        }
        return flag;

    }

}

Please help me 请帮我

There is no need to have an iterator in the remove method, you can pass the current LimitOrder from the create method. remove方法中不需要迭代器,您可以从create方法传递当前的LimitOrder

static ArrayList<LimitOrder> a = new ArrayList<>();

public static void main(String args[]) {
    create();
}

public static void create() {
    LimitOrder l1 = new LimitOrder(1, 100, "145");

    LimitOrder l2 = new LimitOrder(1, 100, "542");

    LimitOrder l3 = new LimitOrder(1, 100, "355");

    a.add(0, l1);
    a.add(1, l2);
    a.add(2, l3);

    Iterator<LimitOrder> i = a.iterator();
    while (i.hasNext()) {
        LimitOrder l = i.next();
        if (remove(l))
        {
            System.out.println("Removed "+l.orderID);
            i.remove();
        }
    }

}

public static boolean remove(LimitOrder l) {
    if (l.orderID.equals("542")) {
        flag = true;
    }
}

更新函数remove()以接收并检查当前迭代器值。

The problem is that you are removing elements from the list that do not match. 问题是您要从列表中删除不匹配的元素。 And that is happening because your test to decide if an element should be removed is wrong. 这种情况正在发生,因为您决定是否应删除元素的测试是错误的。 You should be testing if this element matches ... not if any element matches. 您应该测试元素是否匹配...如果任何元素匹配则不应该。

The fix is to change the test to test just the current element; 修复方法是更改​​测试以仅测试当前元素; eg 例如

    ...
    Iterator<LimitOrder> i = a.iterator();
    while (i.hasNext()) {
        LimitOrder l = i.next();
        if (isMatch(l)) {
            i.remove();
            System.out.println("Removed " + l.orderID);
        }
    }
    ...

public static boolean isMatch(LimitOrder l) {
    return l.orderID.equals("542");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM