简体   繁体   English

从对象的收集/更改字段中删除项目

[英]Removing Item from Collection / Changing field of Object

public void searchOwner(List<Appointments> appts, String owner) {
    Appointments theOne = null;
    for (Appointments temp : appts) {
        if (owner.equalsIgnoreCase(temp.owner.name)) {
            System.out.println(temp.data);
            temp.setResolved(true);
        }
    }
}

public void checkRemoval() {
    for (Appointments appts : appointments) {
        if (appts.resolved == true) {
            appointments.remove(appts);
        }

//Iterator method used before enhanced for-loop
    public void checkRemovalI(){
    Iterator<Appointments> it = appointments.iterator();
    while(it.hasNext()){
        if(it.next().resolved = true){
            it.remove();
        }
    }
}

So far this is where I am encountering my problem. 到目前为止,这是我遇到问题的地方。 I am trying to check the arrayList of Appointments and see if the field (resolved) is set to true, however I am receiving an ConcurrentModification exception during the searchOwner method when trying to set resolved = to true. 我正在尝试检查约会的arrayList,并查看字段(已解决)是否设置为true,但是在尝试将resolve =设置为true时,我在searchOwner方法期间收到了ConcurrentModification异常。 I've tried using an Iterator in checkRemoval instead of an enhanced for-loop however that didn't help either. 我已经尝试在checkRemoval中使用迭代器,而不是增强的for循环,但是这也没有帮助。 I really only need to get the part where the appointment is set to true to work, the checkRemoval seemed to be working early before implementing the changing of the boolean resolved. 我真的只需要获得将约会设置为true的部分即可工作,在实现更改后的布尔值解析之前,checkRemoval似乎早在工作。 Any help will be greatly appreciated, thank you. 任何帮助将不胜感激,谢谢。

I'm willing to bet that the ConcurrentModificationException is not being caused where you say it is, but rather in checkRemoval() , which you're probably calling before the line you mention where you set resolved to true, hence your confusion. 我愿意打赌, ConcurrentModificationException ,你说,这是不被引起的,而是在checkRemoval()你很可能前行打电话给你提你设置resolved为true,因此你的困惑。

I only say this because: 我之所以这样说是因为:

for (Appointments appts : appointments) {
    if (appts.resolved == true) {
        appointments.remove(appts);
    }
}

is a blatant concurrent modification. 是公然的并发修改。 You cannot remove elements from a collection while you are iterating through it in a loop. 在循环中循环访问集合时,不能从集合中删除元素。 Instead, you need to use an iterator : 相反,您需要使用迭代器

public void checkRemoval() {
    Iterator<Appointment> apptsIterator = appointments.iterator();
    while (apptsIterator.hasNext()){ 
        if (appts.next().resolved == true) 
            apptsIterator.remove(); //removes the last element you got via next()
    }

The ConcurrentModification exception is thrown, using the for loop, where the Collection gets modified. 使用for循环引发ConcurrentModification异常,在该循环中修改Collection。 So the issue need not be the code that you haave posted. 因此,问题不必是您发布的代码。 You might be having a loop over the appts List, which is calling this function. 您可能正在调用此函数的appts列表上有一个循环。 Posting more of your code might help. 发布更多代码可能会有所帮助。

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

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