Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我总是得到一个';找不到符号';Java尝试将堆栈实现为数组时出错_Java_Arrays_Stack - Fatal编程技术网

我总是得到一个';找不到符号';Java尝试将堆栈实现为数组时出错

我总是得到一个';找不到符号';Java尝试将堆栈实现为数组时出错,java,arrays,stack,Java,Arrays,Stack,因此,对于赋值,我需要使用数组实现堆栈。我已经弄明白了所有的代码,但出于某种原因,每当我尝试使用我的char数组时,Java都会说找不到它。这是我的堆栈类 public class Stack { private int top; public Stack () { char []charArray = new char [50]; top = -1; } public void push(char c) {

因此,对于赋值,我需要使用数组实现堆栈。我已经弄明白了所有的代码,但出于某种原因,每当我尝试使用我的char数组时,Java都会说找不到它。这是我的堆栈类

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);
    }
}

有人知道我为什么会犯这些错误吗?它不喜欢push和top中引用charArray的行。

数组变量
charArray
在构造函数中声明为局部变量,因此其他方法看不到它

您可以将其声明为成员字段:

 private int top;
 private char[] charArray;

 public Stack ()
 {
    charArray = new char [50];
    top = -1;
 }

数组变量
charArray
在构造函数中声明为局部变量,因此其他方法不可见

您可以将其声明为成员字段:

 private int top;
 private char[] charArray;

 public Stack ()
 {
    charArray = new char [50];
    top = -1;
 }

数组变量
charArray
在构造函数中声明为局部变量,因此其他方法不可见

您可以将其声明为成员字段:

 private int top;
 private char[] charArray;

 public Stack ()
 {
    charArray = new char [50];
    top = -1;
 }

数组变量
charArray
在构造函数中声明为局部变量,因此其他方法不可见

您可以将其声明为成员字段:

 private int top;
 private char[] charArray;

 public Stack ()
 {
    charArray = new char [50];
    top = -1;
 }

离题,但我看到您没有
isFull
类型的方法,最好在
charArray
中添加可调整大小的功能,以避免可能的
ArrayIndexOutOfBoundsException
离题,但我看到您没有
isFull
类型的方法,最好将可调整大小添加到
charArray
以避免可能的
ArrayIndexOutOfBoundsException
主题外,但我发现您没有
isFull
类型的方法,最好将可调整大小添加到
charArray
以避免可能的
ArrayIndexOutOfBoundsException
主题外,但我发现您没有
isFull
类型的方法,最好在
charArray
中添加可调整大小的功能,以避免出现可能的
ArrayIndexOutOfBoundsException