Java 正在替换LinkedList中的节点,而不是实际替换任何内容

Java 正在替换LinkedList中的节点,而不是实际替换任何内容,java,linked-list,doubly-linked-list,Java,Linked List,Doubly Linked List,我目前正在编写一个双链表的实现,在将节点设置为某个值时遇到了问题。因为节点中的数据是最终的,所以我需要替换整个节点,但是我为其编写的代码似乎一直在工作,直到它退出该方法,并且原始列表根本没有更改。代码里有我遗漏的东西吗 `public void set(int index, String item) { if (index < 0 || index >= this.size) { throw new IndexOutOfBoun

我目前正在编写一个双链表的实现,在将节点设置为某个值时遇到了问题。因为节点中的数据是最终的,所以我需要替换整个节点,但是我为其编写的代码似乎一直在工作,直到它退出该方法,并且原始列表根本没有更改。代码里有我遗漏的东西吗

`public void set(int index, String item) {
            if (index < 0 || index >= this.size) {
                throw new IndexOutOfBoundsException();
            }
            Node curr = this.front;
            for (int i = 0; i < index; i++) {
                curr = curr.next;
            }
            Node temp = new Node(item);
            temp.prev = curr.prev;
            temp.next = curr.next;
            curr = temp;
        }`
`public void set(int索引,字符串项){
如果(索引<0 | |索引>=此.size){
抛出新的IndexOutOfBoundsException();
}
节点curr=this.front;
对于(int i=0;i
您还应该更改上一个节点的“
next
”和下一个节点的“
prev
”。假设
prev
cur
的上一个节点,
next
cur
的下一个节点

Node temp = new Node(item);
temp.prev = prev;
temp.next = next;
next.prev = temp;
prev.next = temp;
你应该处理边缘问题。

你可以

 System.out.println(curr.val);
你会发现印刷品实际上和物品是一样的

但是当你回来的时候,你会发现没有什么变化

您只需找到curr的pre节点并设置pre.next=curr(其中已更改)

`public void set(int索引,字符串项){
如果(索引<0 | |索引>=此.size){
抛出新的IndexOutOfBoundsException();
}
节点curr=this.front;
对于(int i=0;i
如果节点中的数据是最终数据,那么您如何期望原始列表没有任何更改?我可以在调用set方法之前和之后打印列表,没有任何更改您是否共享“Node”类?如果temp.next或temp.prev为null,这会中断,这些是预期的边缘情况吗?是的,例如,如果您的
索引
0
size-1
,如果列表中只有一个或两个节点,该怎么办?这些是边缘情况,您应该仔细处理。
temp.next
temp.prev
始终为空,因为它是一个新节点,您的意思可能是
prev
next
`public void set(int index, String item) {
        if (index < 0 || index >= this.size) {
            throw new IndexOutOfBoundsException();
        }
        Node curr = this.front;
        for (int i = 0; i < index; i++) {
            curr = curr.next;
        }
        Node temp = new Node(item);
        temp.prev = curr.prev;
        temp.next = curr.next;
        curr = temp;
        //here you need to set maybe 
         // Node tmp=curr.prev;
         // tmp.next=curr;
    }`