我想删除与 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;
}
}
请帮我