java链表子列表方法

java链表子列表方法,java,linked-list,nodes,sublist,Java,Linked List,Nodes,Sublist,我正在尝试编写一个子列表方法,该方法返回一个由当前对象列表组成的列表,包括索引fromIndex和toIndex之间的列表 例如,如果我有一个由 911 20 23 28 30 我调用了子列表(1,4),我应该得到一个新的 11202328 返回 对于我的subList()方法,我尝试运行它,但最终进入了一个无限循环。当下一个节点为空时,它应该退出循环,但这并没有发生,所以我需要帮助弄清楚为什么会发生这个无限循环,以及我能做些什么。感谢您的帮助 private class Node<N e

我正在尝试编写一个子列表方法,该方法返回一个由当前对象列表组成的列表,包括索引
fromIndex
toIndex
之间的列表

例如,如果我有一个由

911 20 23 28 30

我调用了
子列表(1,4)
,我应该得到一个新的

11202328

返回

对于我的subList()方法,我尝试运行它,但最终进入了一个无限循环。当下一个节点为空时,它应该退出循环,但这并没有发生,所以我需要帮助弄清楚为什么会发生这个无限循环,以及我能做些什么。感谢您的帮助

private class Node<N extends Comparable<N>> {
    private N data;
    private Node<N> next;
}

private Node<L> head;

public List() {
    head = null;
}   

private void add(Node<L> node) {
        if(head == null) {
            head=node;
        } else {
            getFinal().next = node;
        }
    }

public Node<L> getFinal(){
    Node<L> node = head;
    while (node.next != null) {
        node = node.next;
    }
    return node;
}

public int size() {
    if (head == null) return 0;
    int counter = 0;
    for (Node<L> curr = head; curr != null; curr = curr.next)
        counter++;
    return counter;
}

public List<L> subList(int fromIndex, int toIndex) throws IndexOutOfBoundsException {
    if(fromIndex < 0 || fromIndex > size()-1 || toIndex < 0 || toIndex > size()-1) { 
         throw new IndexOutOfBoundsException();
    }

    List<L> n = new List<L>();
    Node<L> startNode = head;
    int counter = 0;
    while(startNode != null) {
        if(counter >= fromIndex && counter <= toIndex) { //infinite loop happens here
            n.add(startNode);
        }
        startNode=startNode.next;
        counter++;
    }

    return n;
}
私有类节点{
私有数据;
私有节点下一步;
}
专用节点头;
公开名单(){
head=null;
}   
专用void添加(节点){
if(head==null){
头部=节点;
}否则{
getFinal().next=节点;
}
}
公共节点getFinal(){
节点=头部;
while(node.next!=null){
node=node.next;
}
返回节点;
}
公共整数大小(){
if(head==null)返回0;
int计数器=0;
for(节点curr=head;curr!=null;curr=curr.next)
计数器++;
返回计数器;
}
公共列表子列表(int-fromIndex,int-toIndex)引发IndexOutOfBoundsException{
如果(从索引<0 | |从索引>大小()-1 | |到索引<0 | |到索引>大小()-1){
抛出新的IndexOutOfBoundsException();
}
列表n=新列表();
节点startNode=头部;
int计数器=0;
while(startNode!=null){

如果(counter>=fromIndex&&counter,我认为您应该创建一个新节点,而不是将原始列表的节点添加到新列表(即子列表):

现在,假设您调用
子列表(1,2)
。这将调用
n.add(2)
。这将调用
getFinal()
,但
getFinal()
仍将返回3(而不是2),因为2.next仍然指向3。 所以在3之后加上,得到一个无限循环,因为2.next=3:

1 -> 2 -> 3 -> 2
     ^         ^ <-- Cycle in your list...
1->2->3->2

^^如果我们只看一下您的子列表方法的while循环,您的退出条件就没有意义了。您有:

while(startNode != null) {
    if(counter >= fromIndex && counter <= toIndex) { //infinite loop happens here
        n.add(startNode);
    }
    startNode=startNode.next;
    counter++;
}
while(startNode!=null){

如果(counter>=fromIndex&&counter似乎缺少类声明,某些方法不在类内。您不显示创建列表并为列表赋值的代码。是否确实要向列表中添加一个节点,该节点的
下一个
字段已设置为列表中的其他内容?作为补充,用于完成如果toIndex1 -> 2 -> 3 -> 2
     ^         ^ <-- Cycle in your list...
while(startNode != null) {
    if(counter >= fromIndex && counter <= toIndex) { //infinite loop happens here
        n.add(startNode);
    }
    startNode=startNode.next;
    counter++;
}
while(startNode != null && counter <= toIndex) {
    if(counter >= fromIndex) {
        n.add(startNode);
    }
    startNode=startNode.next;
    counter++;
}