我的问题是反转存储字符串的链接列表。我知道我的反转链表的方法已经有效,我可以使用相同的方法来反转整数列表,但在反转字符串时它不起作用。它给了我以下错误,我不确定为什么。
这是我的 printInReverse() 方法
public void printInReverse(Node L)
{
if (L.next == null)
{
System.out.print(L.data + " ");
}
else
{
printInReverse(L.next);
System.out.print(L.data + " ");
}
}
这是我调用该方法的方式
System.out.print("Reversed string: "); myList.printInReverse(myList.top);
这是我收到的错误消息
ReverseString_Scott_Robinson.java:24: error: cannot find symbol
System.out.print("Reversed string: "); myList.printInReverse(myList.end);
^
symbol: method
printInReverse(Stack_Scott_Robinson<String>.Node<String>)
location: variable myList of type Stack_Scott_Robinson<String>
Note: ReverseString_Scott_Robinson.java uses unchecked or unsafe operations.
最佳答案
您似乎正在尝试调用 List 对象上的方法。该接口(interface)没有 printInReverse 函数,因此这是一个错误。只需像这样调用该函数即可:
printInReverse(myList.top);
相对于:
myList.printInReverse(myList.top);
关于java - 用链表反转字符串方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58188369/