在可比数组中找不到符号

Cannot find symbol on comparable array

我必须从给定的界面构建一堆可比较的对象。 在 class 中,这是我的构造函数:

public S()
{
   Comparable[] arr = new Comparable[INITSIZE];
   size = 0;
}

现在,在数组出现的每个方法中,例如:

public void push(Comparable x)
{
   arr[size++] = x;
}

我在编译时遇到无法找到与 arr 相关的符号错误。为什么?

I get cannot find symbol error related to arr while compiling.

在 class 内部但在任何方法或构造函数外部声明 arr

public class S{

    Comparable[] arr;  
}

并在构造函数中初始化它。

public S()
{
    arr = new Comparable[INITSIZE];
}

否则 arr 对其他方法不可见,编译时你会得到 cannot find symbol error related to arr 因为它是构造函数中的局部变量。

public class S{

        Comparable[] arr = null; 

        public S()
        {
             arr = new Comparable[INITSIZE];
        }

    }