Java数组操作

Java数组操作,java,arrays,Java,Arrays,我正在使用Netbeans编写Java代码,当我想访问另一个类中的数组(计算平均值并将其设置为textfield2)时,我必须将插入textField1的输入(学生分数)存储到数组中 我收到此错误“找不到符号变量:数组的名称”,是否有我应该导入的包或我做错了什么?我将感谢你的帮助 不看代码就很难说出你做错了什么。但这里是做这件事的一般方法。假设您有两个这样的类: class OtherClass{ private int accessMe = 10; public int ac

我正在使用Netbeans编写Java代码,当我想访问另一个类中的数组(计算平均值并将其设置为textfield2)时,我必须将插入textField1的输入(学生分数)存储到数组中


我收到此错误
“找不到符号变量:数组的名称”
,是否有我应该导入的包或我做错了什么?我将感谢你的帮助

不看代码就很难说出你做错了什么。但这里是做这件事的一般方法。假设您有两个这样的类:

class OtherClass{

    private int accessMe = 10;
    public int accessMe2 = 20;
    public static int accessMe4 = 50;

    public OtherClass(){

        int accessMe3 = 40;

    }

}
要访问OtherClass的变量,请执行以下操作:

class Access{

    public static void main(String args[]){

        OtherClass other = new OtherClass();            

        //this is not valid - because accessMe is declared as private in OtherClass
        //meaning you can only use/access accessMe variable inside the scope of OtherClass
        //If you had to access this variable, you would put a getter/setter in your OtherClass implementation.
        int accessMe = other.accessMe;

        //this is valid - because there is no particular restriction on accessMe2 inside OtherClass
        int accessMe2 = other.accessMe2;

        //this is not valid since accessMe3 is declared inside the local scope (which is the contructor of OtherClass)
        //You do not have access to accessMe3 except inside the constructor
        int accessMe3 = other.accessMe3;

        //this is valid since accessMe4 is a static variable
        int accessMe4 = OtherClass.accessMe4;

    }


}
如您所见,这在很大程度上取决于您如何声明数组。你宣布它是私人的吗?公众?静止的受保护的?在哪里申报?该类是否属于完全不同的项目?(在这种情况下,您必须导入)


您还可以将变量类型更改为示例中所需的任何类型。

在问题中输入您的工作代码。这将大大有助于确定您做错了什么。非常感谢您的帮助我将数组声明为局部变量,现在我已将其更改为全局变量。