Java数组操作

时间:2018-12-12 17:25:25

标签: java arrays

我正在使用Netbeans编写Java代码,当我要访问其他类中的数组时,必须将插入textField1中的输入(学生标记)存储到Array中(以计算平均值并将其设置为textfield2)。

我收到此错误"Cannot find Symbole variable:name_of_Array",是否应该导入任何软件包,或者我做错了什么?感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

不用看代码就不必说自己在做什么错。但是,这是执行此操作的一般方法。可以说您有两个这样的类:

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;

    }


}

如您所见,它在很大程度上取决于您声明数组的方式。您是否宣布为私有?上市?静态的?受保护?声明在哪里?该课程属于一个完全不同的项目吗? (在这种情况下,您必须导入)

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