简体   繁体   English

我在尝试将堆栈实现为数组的过程中不断收到“找不到符号”错误

[英]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.

相关问题 尝试进行构造函数链接时,我不断收到“错误:找不到符号” - I keep getting “error: cannot find symbol” when trying to do constructor chaining 我不断收到找不到符号方法compareTo ERROR - I keep getting Cannot find symbol method compareTo ERROR 我不断收到错误消息说找不到符号-可变键盘 - I keep getting a error saying cannot find symbol - variable keyboard 为什么为什么不断出现“找不到符号”错误? - Why do I keep getting “cannot find symbol” error? 我在println中不断收到“错误:找不到符号” - I keep getting “error:Cannot find symbol” in println 每当编译时,我都会不断收到无法找到符号的错误 - Whenever compile, I keep getting the error that it cannot find the symbol 使用Comparable时,我不断获取找不到符号错误 - I keep on getting Cannot find Symbol Error when using Comparable JAVA:为什么继续出现错误:尝试访问另一个类文件时找不到符号? - JAVA: Why do I keep getting the error: cannot find symbol when I try to access another class file? 为什么在Java中出现“找不到符号”错误? - Why am I getting a “cannot find symbol” error in Java? 为什么我收到错误:在 java 中找不到符号? - Why am I getting error: cannot find symbol in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM