简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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