问题
I'm trying to add a list node at a specified index recursively. By that I mean the List class addAtRec() calls addAtRec() in the ListNode class, that method is supposed to be recursive.
This is what I did:
List:
public class List implements Cloneable {
private ListNode firstNode;
private ListNode lastNode;
private String name;
private int counter;
public List(){
this("list");
}
public void addAtRec(Object obj, int k)
{
if(firstNode != null)
firstNode.addAtRec(obj, k, firstNode);
}
}
That's of course only the relevant parts...
ListNode:
public class ListNode implements Cloneable {
Object data;
ListNode nextNode;
public ListNode(Object o){
this(o,null);
}
public ListNode(Object o,ListNode node){
data=o;
nextNode=node;
}
public void addAtRec(Object obj, int k, ListNode current) throws ListIndexOutOfBoundsException {
if(current.nextNode == null && k != 0)
throw new ListIndexOutOfBoundsException(); //line 47
if(k == 0)
{
ListNode l = new ListNode(obj);
l.nextNode = current.nextNode;
current.nextNode = l;
}
current = current.nextNode;
addAtRec(obj, k, current); //line 55
k--;
}
ListIndexOutOfBoundsException:
public class ListIndexOutOfBoundsException extends RuntimeException {
}
my main() method:
String s1 = "Objects";
String s2 = "to";
String s3 = "check";
String s4 = "are";
String s5 = "strings";
List list = new List("My list");
list.insertAtBack(s1);
list.insertAtBack(s2);
list.insertAtBack(s3);
list.insertAtBack(s4);
list.insertAtBack(s5);
list.addAtRec(s3, 2);
and the error:
Exception in thread "main" ListIndexOutOfBoundsException
at ListNode.addAtRec(ListNode.java:47)
at ListNode.addAtRec(ListNode.java:55)
at ListNode.addAtRec(ListNode.java:55)
at ListNode.addAtRec(ListNode.java:55)
at ListNode.addAtRec(ListNode.java:55)
at List.addAtRec(List.java:158)
What did I do wrong? Thanks for your time and answers.
回答1:
You have two errors in your recursive method:
Before calling
addAtRec(obj, k, current);you should decreasekby 1, so it would be better to callk--before this line.Once you have reached the base case (when
k == 0)and executed the logic to add the new node, your recursive method must stop, probably with a simplereturn;statement. In this case, you're not stopping it so you will call it every time until get to the end of your list.
Based on these 2 advices, your code should look like:
public void addAtRec(Object obj, int k, ListNode current)
throws ListIndexOutOfBoundsException {
//always use braces even for single line block of code
if(current.nextNode == null && k != 0) {
throw new ListIndexOutOfBoundsException();
}
if(k == 0) {
ListNode l = new ListNode(obj);
l.nextNode = current.nextNode;
current.nextNode = l;
//stopping the recursion
return;
}
current = current.nextNode;
//decrease k by 1 before calling your method recursively
k--;
addAtRec(obj, k, current);
}
This is not part of the main problem, but IMO your methods to add nodes in the list should belong to the List class and not to theListNode. Remember that the data structure that holds the nodes and decide how to tie them will be the List, not the nodes by themselves.
来源:https://stackoverflow.com/questions/17261687/recursively-add-a-node-at-a-certain-index-on-linked-list