[英]I keep getting a 'cannot find symbol' error in Java trying to implement a Stack as an array
So for an assignment I need to implement a Stack using an array. 因此,对于分配,我需要使用数组实现堆栈。 I have all of the code figured out, but for some reason whenever I try to use my char array, Java says it can't find it.
我已经弄清楚了所有代码,但是由于某种原因,每当我尝试使用char数组时,Java都说找不到它。 Here is my stack class.
这是我的堆栈类。
public class Stack
{
private int top;
public Stack ()
{
char []charArray = new char [50];
top = -1;
}
public void push(char c)
{
top++;
charArray[top] = c;
}
public void pop()
{
top--;
}
public char top()
{
return charArray[top];
}
public void makeNull()
{
top=-1;
}
public boolean isEmpty()
{
return (top==-1);
}
}
Does anyone have any idea why I'm having these errors? 有谁知道为什么我会出现这些错误? It doesn't like the lines in push and top where I reference charArray.
它不喜欢我引用charArray的push和top中的行。
The array variable charArray
is declared as a local variable inside the constructor, and thus is not visible to the other methods. 数组变量
charArray
在构造函数内部声明为局部变量,因此其他方法不可见。
You can declare it as a member field: 您可以将其声明为成员字段:
private int top;
private char[] charArray;
public Stack ()
{
charArray = new char [50];
top = -1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.