在Java中删除DoubleLinkedList中的对象时出现问题

在Java中删除DoubleLinkedList中的对象时出现问题,java,Java,我正在努力使用这个删除(objectobj)方法。我试过很多不同的方法,这是我能得到的最接近的方法。要删除的项是传入对象之前的项: public boolean remove(Object obj) { if (!this.contains(obj)) return false; //if we get to here, we know that the list contains obj somewhere //change the links and

我正在努力使用这个删除(objectobj)方法。我试过很多不同的方法,这是我能得到的最接近的方法。要删除的项是传入对象之前的项:

public boolean remove(Object obj)
{
    if (!this.contains(obj))
        return false;

    //if we get to here, we know that the list contains obj somewhere
    //change the links and return true at the end.
    else if (head.data.equals(obj))    //is it at the front?
    {
        this.removeFirst();
        return true;
    }

    else    //not at front, so traverse the list
    {
        DLLNode<E> doomed = head;
        while (!doomed.data.equals(obj))
            doomed = doomed.next;

        //now that it is found, find the node in front of it
        DLLNode<E> inFront = head;
        while (inFront.next != doomed)
            inFront = inFront.next;

                    //
                    // HERE IS THE PROBLEM & MY EXPLANATION BEHIND IT
                    //
        // locate the prev locator of the following node of the obj and set it equal to the beginning (prev) of the node in front of the obj removed
        inFront.next.prev = inFront.prev;
        // locate the next locator of the preceding node of the obj and set it equal to the end (next) of the node in front of the obj removed
        inFront.prev.next = inFront.next;

        //also...if the one that was deleted was the tail, we must reset the tail
        if (doomed == tail)
            tail = inFront;
    }

    return true;   //found it; links have been changed

}

现在,从左向右读取DLL时,将删除与对象匹配的正确节点。但是,从右到左读取DLL时,对象会重新出现在相同的位置。

Edit: 是的,查找元素的代码是正确的。但是在删除部分,您认为'inFront'是前一个节点,因此它删除了错误的节点。 现在删除。在删除之前,您有如下内容:

现在,您必须将
inFront.next
设置为
destine.next

inFront.next=doomed.next;
您的列表现在如下所示:

下一步是将
destine.next.prev
设置为
inFront

doomed.next.prev=inFornt;
最终统计数据:


由于没有对注定要失败的对象的引用,垃圾收集器将从内存中删除它。

已解决!这是一个愚蠢的错误。我没有考虑另一个案子。我理解,如果没有向社区提供的所有信息,我无法期望任何人能够充分了解我的问题并提供帮助。这件我请客。为了公开羞辱自己,并说服其他人在将来提供必要的信息,我将解释问题和解决方案

我最初的问题是,要删除的节点是请求对象前面的节点。解决方法如下:

inFront.next = doomed.next;
doomed.next.prev = inFront;
然后,我没有考虑这样一个事实,即在列表末尾移除的对象将需要一个特殊情况来避免诸如不移除或破坏与其他节点的连接之类的问题。通过创建if语句解决了这一问题:

if (doomed != tail) {
    inFront.next = doomed.next;
    doomed.next.prev = inFront;
} else {
    inFront.next = null;
}

谢谢大家!!我以后一定会更好地组织我的问题。

我尝试了你的建议,但没有成功。它最终没有删除我测试中的任何内容。谢谢你(:@baskets小丑可能是因为你使用了
equals()
在第一个while循环中。尝试将其更改为
!=
。我已对我的代码和问题进行了调整。现在已接近预期输出,但现在对所有情况都准确无误。我怀疑这与连接前面值的prev定位器有关。这太棒了!非常感谢您花时间这样布置它。我对你的评论投了更高的票,但我还没有得到街头的信任,所以我只是想建议你自己做的事情。也许你像以前一样把它作为一个单独的测试来做,但是你可以在else…
tail=inFront
中做。另外,你也不需要在
else
中使用特例。你可以n只需执行
inFront.next=注定。next
无条件,因为如果
注定
在结尾,那么
注定。next
将是
空的
,因此
inFront.next=注定。next
做了正确的事情。这很有意义!谢谢(:刚刚做了那个更改
inFront.next = doomed.next;
doomed.next.prev = inFront;
if (doomed != tail) {
    inFront.next = doomed.next;
    doomed.next.prev = inFront;
} else {
    inFront.next = null;
}