gpt4 book ai didi

java 说 'cannot find symbol' 尽管方法是在类中声明的

转载 作者:行者123 更新时间:2023-12-01 19:31:43 25 4
gpt4 key购买 nike

我正在编写一个使用链表的堆栈实现程序。在其中,我创建了一个类 Node其中包含所有 getter 和 setter,我还有另一个类 Stack它控制堆栈操作。我试图显示堆栈元素,类 Stack 中有一个单独的函数如下:

class Stack{
Node start;
int size;
public void viewStack(){
if(isEmpty()){
System.out.println("Stack is Empty");
} else {
Node s = start;
while(s.getNext()!=null){
System.out.print(s.getData()+" ");
s = s.getNext();
}
}
}
}

其中,isEmpty()根据情况返回 true 或 false。 getData()定义在类 node 中如下:

class Node{
Node next;
int data;
public int getdata(){
return data;
}
}

驱动代码如下:

public class StackDemo{
public static void main(String args[]){
Stack st = new Stack();
st.viewStack();
}
}

我收到错误 cannot find symbol错误指向 s.getData()方法。为什么我会收到此错误?请帮忙。

最佳答案

因为你的方法是getdata而不是getData

关于java 说 'cannot find symbol' 尽管方法是在类中声明的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59520940/

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