gpt4 book ai didi

java - 我的 LinkedList 的 set 方法有什么问题

转载 作者:行者123 更新时间:2023-12-02 05:46:22 24 4
gpt4 key购买 nike

我正在尝试实现一个链表。除了 set 方法之外,我的所有方法都有效。它只应该更改或替换给定索引处的元素。但是在索引中设置元素后,列表中的其余元素都为空,任何人都可以指出我在 set 方法中做错了什么吗?

        public class LArrayList<E> {

private static class Node<E> {

Node<E> next;

E data;

public Node(E dataValue) {
next = null;
data = dataValue;
}

@SuppressWarnings("unused")
public Node(E dataValue, Node<E> nextValue) {
next = nextValue;
data = dataValue;
}

public E getData() {
return data;
}

public Node<E> getNext() {
return next;
}

public void setNext(Node<E> nextValue) {
next = nextValue;
}
}

private Node<E> head;
private static int listCount;

public LArrayList() {

head = new Node<>(null);
listCount = 0;
}

public void add(int index, E e) throws IndexOutOfBoundsException{

if (index < 0 || index >= listCount + 1) {

throw new IndexOutOfBoundsException("Bad index, please use index within range");
}
else{


Node<E> positionTemp = new Node<>(e);
Node<E> positionCurrent = head;

for (int i = 0; i < index && positionCurrent.getNext() != null; i++) {
positionCurrent = positionCurrent.getNext();
}

positionTemp.setNext(positionCurrent.getNext());
positionCurrent.setNext(positionTemp);
listCount++;
}
}

public E set(int index, E e) throws IndexOutOfBoundsException {

if (index < 0 || index >= listCount)
throw new IndexOutOfBoundsException("Bad index, please use index within range");


Node<E> positionTemp = new Node<>(e);
Node<E> positionCurrent = head;

for (int i = 0; i < index; i++) {
positionCurrent = positionCurrent.getNext();
}


positionCurrent.setNext(positionTemp);

return positionCurrent.getData();

}

public E get(int index) throws IndexOutOfBoundsException
{

if (index < 0 || index >= listCount)
throw new IndexOutOfBoundsException("Bad index, please use index within range");

Node<E> positionCurrent = head.getNext();
for (int i = 0; i < index; i++) {
if (positionCurrent.getNext() == null)
return null;

positionCurrent = positionCurrent.getNext();
}
return positionCurrent.getData();
}



public static void main(String[] args) {

LArrayList<String> aa = new LArrayList<String>();
aa.add(0, "0");
aa.add(1, "1");
aa.add(2, "2");
aa.add(3, "3");
aa.add(4, "4");
aa.add(5, "5");


System.out.println("The contents of AA are: " + aa);

aa.set(0, "a");


System.out.println("The contents of AA are: " + aa);



System.out.println(aa.get(0));

System.out.println(aa.get(1));

System.out.println(aa.get(2));
System.out.println(aa.get(3));

System.out.println(aa.get(4));

System.out.println(aa.get(5));

//OUTPUT IS: The contents of aa are: [0][1][2][3][4][5]
//The contents of AA are: [a][b][c][d][e]
//a
//A
//null
//null
//null
//null

}

}

最佳答案

快速查看代码后:

 positionCurrent.setNext(positionTemp);

您不需要将 positionTemp 链接到它后面的元素吗?

关于java - 我的 LinkedList 的 set 方法有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24030884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com