Java 递归地从链表中删除元音-逻辑错误
我正在使用递归从一个名为MySB2的StringBuilder功能的节点链表(data:char)中删除元音-只是想寻找一种方法来挑战自己,更好地理解链表的递归。 但我遇到了一个逻辑错误。我添加了一些打印语句,表明我的方法是删除元音,保留辅音,这很好。但是,当我去打印变异的链表时(通过我功能齐全的toString()方法),它会执行以下操作: 之前:幸福就是爱 之后:Hpnsslbutv 这是我的密码:Java 递归地从链表中删除元音-逻辑错误,java,recursion,linked-list,Java,Recursion,Linked List,我正在使用递归从一个名为MySB2的StringBuilder功能的节点链表(data:char)中删除元音-只是想寻找一种方法来挑战自己,更好地理解链表的递归。 但我遇到了一个逻辑错误。我添加了一些打印语句,表明我的方法是删除元音,保留辅音,这很好。但是,当我去打印变异的链表时(通过我功能齐全的toString()方法),它会执行以下操作: 之前:幸福就是爱 之后:Hpnsslbutv 这是我的密码: //The calls in my main are below public static
//The calls in my main are below
public static void main(String[] args)
{
System.out.println("\nTesting remove vowels method");
b1 = new MySB2("Happiness is All About Love");
System.out.println("Before: " + b1);
b1.removeVowels();
System.out.println("After: " + b1);
}
//These methods below are within the linked list class, MySB2
public class MySB2
{
public MySB2 removeVowels()
{
noVowels(firstNode, 0);
//firstNode is the reference to the 1st node in the linked list
return this;
}
private MySB2 noVowels(CNode curr, int i)
{
if(curr != null)
{
if(isAVowel(curr) != true)
{
System.out.println("noVowels() keeping: " + curr.data);
noVowels(curr.next, i++);
return this;
}
else if(isAVowel(curr) == true)
{
i++;
CNode nodeBefore = getNodeAt(i-1);
CNode nodeAfter = curr.next;
System.out.println("noVowels() removing: " + curr.data);
nodeBefore.next = nodeAfter;
length--;
}
noVowels(curr.next, i++);
}
return this;
}
private boolean isAVowel(CNode curr)
{
char c = Character.toLowerCase(curr.data);
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
{
//System.out.println("isAVowel() is detecting: " + c);
//returning true for the proper letters
return true;
}
return false;
}
}
如果有人能帮助我理解我的逻辑错误,我将不胜感激。也许能帮我弄清楚怎么不再这样做了。谢谢 当你打电话时
noVowels(curr.next, i++);
您正在将i
的原始值传递给递归方法。您可能希望传递递增的值,这需要
noVowels(curr.next, ++i);
或
选择使用这两个选项中的哪一个取决于您是否希望i
的值在方法返回后仍然递增。但是,您在代码中选择两个选项中的哪一个似乎无关紧要,因为您的方法总是在进行递归调用后立即返回,因此返回后i
的值是多少无关紧要
编辑:
实际上,你的逻辑有几个问题。我无法测试它,但这应该可以:
if(curr != null)
{
if(!isAVowel(curr))
{
System.out.println("noVowels() keeping: " + curr.data);
}
else
{
//i++; don't increment i, since you want to remove the current node
CNode nodeBefore = getNodeAt(i-1);
CNode nodeAfter = curr.next;
System.out.println("noVowels() removing: " + curr.data);
nodeBefore.next = nodeAfter;
length--;
}
return noVowels(curr.next, i+1); // you only need one recursive call
}
return this;
编辑2:
这段代码仍然不能处理第一个节点是元音的情况。我可能仍然感到困惑,我只是尝试将递归调用更改为
noVowels(curr.next,++I)代码>或<代码>noVowels(当前下一步,i+1)
但遇到了StackOverflowerErrorsFraid我在测试“Love”一词时,在“L”处遇到了IndexOutOfBoundsException,即使使用1(第二个索引)作为I变量而不是0(第一个索引)。“你能向我解释一下我的一些逻辑错误吗?”她说,如果没有看到你的链表的全面实施,你是不可能回答的。
if(curr != null)
{
if(!isAVowel(curr))
{
System.out.println("noVowels() keeping: " + curr.data);
}
else
{
//i++; don't increment i, since you want to remove the current node
CNode nodeBefore = getNodeAt(i-1);
CNode nodeAfter = curr.next;
System.out.println("noVowels() removing: " + curr.data);
nodeBefore.next = nodeAfter;
length--;
}
return noVowels(curr.next, i+1); // you only need one recursive call
}
return this;