简体   繁体   English

从Java中的链接列表中删除所有错误的元素

[英]removing all false elements from a linked list in java

I have a linked list of fish data shown here 我这里显示了鱼类数据的链接列表

 //create linked list of fish data called fl
    LinkedList<FishData> fl = new LinkedList<FishData>();
    //Here are a few data items
    fl.add(new FishData("American Eel ", 9, "Summer/Spring ", 25 , false));
    fl.add(new FishData("Hammerhead Shark ", 0, "All Year ", 36, false));
    fl.add(new FishData("Horseshoe Crab ", 7, "All Year except May ", 60, false));
    fl.add(new FishData("Haddock ", 18, "All Year ", 0, true));
    fl.add(new FishData("Tautog ", 16, "late Spring to end of year ", 3, true));

I have to use an iterator to remove all of the false elements from this list and print it again. 我必须使用迭代器从此列表中删除所有虚假元素,然后再次打印。 I think i understand how it works logically, if my data items were only comprised of booleans i think what i did would work, however i dont know how to look at just the boolean field of the element and remove it if its false. 我认为我了解它在逻辑上是如何工作的,如果我的数据项仅由布尔值组成,我认为我所做的就可以,但是我不知道如何仅查看元素的布尔值字段,如果其值为false则将其删除。 this is what i tried and got a class cast exception, i know what that means i just dont know how to fix it. 这就是我尝试过并得到类强制转换异常的原因,我知道那意味着我只是不知道如何解决它。

Iterator itr = fl.iterator();
    while (itr.hasNext()) {
        boolean w = (boolean) itr.next();
        if (w = false)
            itr.remove();
    }

UPDATE: 更新:

so i edited the code and i now have this 所以我编辑了代码,现在有了这个

 Iterator<FishData> itr = fl.iterator();
    while (itr.hasNext()) {
        FishData b = (FishData) itr.next();
        if (!b.w) {
            itr.remove();
        }
    }
    for (FishData element : fl) {
        System.out.println(element);
    }
}

previously i used that for loop to print the whole list but when i use it now, nothing prints 以前我使用for循环打印整个列表,但是当我现在使用它时,什么也没打印

itr.next() will return the next element in your LinkedList , which will be a FishData object. itr.next()将返回LinkedList的下一个元素,该元素将是FishData对象。 You cannot cast this to a boolean variable. 您不能将其强制转换为布尔变量。 Instead you want to access the boolean variable of the fishData object. 相反,您想访问fishData对象的布尔变量。 So assuming that your FishData class has a someCondition boolean variable you can do: 因此,假设您的FishData类具有someCondition布尔变量,则可以执行以下操作:

while (itr.hasNext()) {
    boolean w = itr.next().someCondition;
    if (w = false)
        itr.remove();
}

Or if it is a private variable and you have a getter method: 或者,如果它是一个private变量,并且您有一个getter方法:

while (itr.hasNext()) {
    boolean w = itr.next().isSomeCondition();
    if (w = false)
        itr.remove();
}

Also the line if(w = false) is incorrect. 另外,行if(w = false)不正确。 = is the assignment operator. =是赋值运算符。 You either need to do if(w == false) or if(!w) . 您需要执行if(w == false)if(!w) Or you could simplify this to: 或者,您可以将其简化为:

while (itr.hasNext()) {
    if (!itr.next().isSomeCondition())
        itr.remove();
}

Additionally you need to create a more specific Iterator : 另外,您需要创建一个更具体的Iterator

Iterator<FishData> itr = fl.iterator();

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

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