繁体   English   中英

我的ArrayList无法识别我添加到列表中的内容

[英]My ArrayList won't recognize what I add to the list

这是代码本身

import java.util.ArrayList;

public class Student {
    private String name;
    private int age;

    public Student (String n, int a) {
        name = n;
        age = a;

    }

    public String toString() {
        return name + " is " + age + " years old";
    }

    ArrayList<Student> rayList = new ArrayList<Student>();
    rayList.add(new Student("Sam", 17));
    rayList.add(new Student("Sandra", 18));
    rayList.add(new Student("Billy", 16));
    rayList.add(new Student("Greg", 17));
    rayList.add(new Student("Jill", 18));

    public static void main(String[] args) {
        System.out.println(rayList.get(0));
    }

}

main方法中缺少某些println命令。 但是,当我尝试将5个学生添加到ArrayList时,出现错误“无法对非静态字段rayList进行静态引用”

您正在尝试在可执行上下文之外执行代码。 只能从方法,静态初始化程序或实例初始化程序(感谢NickC)上下文中执行代码。

尝试将其移入main方法以开始...

public static void main(String[] args) {
    ArrayList<Student> rayList = new ArrayList<Student>();
    rayList.add(new Student("Sam", 17));
    rayList.add(new Student("Sandra", 18));
    rayList.add(new Student("Billy", 16));
    rayList.add(new Student("Greg", 17));
    rayList.add(new Student("Jill", 18));
    System.out.println(rayList.get(0));
}

根据反馈进行了更新

您的第一个错误Cannot make a static reference to the non-static field rayList生成Cannot make a static reference to the non-static field rayList因为rayList没有声明为static ,但是您试图从static上下文中引用它。

// Not static
ArrayList<Student> rayList = new ArrayList<Student>();
// Is static
public static void main(String[] args) {
    // Can not be resolved.
    System.out.println(rayList.get(0));
}

rayList被声明为“实例”字段/变量,这意味着它需要声明类( Student )的实例才有意义。

这可以通过...解决

main创建一个Student实例,然后通过该实例访问它,例如...

public static void main(String[] args) {
    Student student = new Student(...);
    //...
    System.out.println(student.rayList.get(0));
}

我个人不喜欢这样, rayList并不真正属于Student ,它没有任何价值。 您能想象在将任何Student添加到List之前必须创建Student实例吗?

我也不喜欢直接访问实例字段,但这是个人喜好。

使rayList static

static ArrayList<Student> rayList = new ArrayList<Student>();
// Is static
public static void main(String[] args) {
    //...
    System.out.println(rayList.get(0));
}

这是一个可行的选择,但是在被认为是好是坏之前,需要更多的上下文。 我个人认为, static字段可能会导致更多问题,而不是解决的问题,但这再次是个人观点,您的上下文可能会认为是合理的解决方案。

或者,您可以在static方法的上下文中创建List的本地实例,如第一个示例所示。

public static void main(String[] args) {
    ArrayList<Student> rayList = new ArrayList<Student>();
    //...
    System.out.println(rayList.get(0));
}

就像我说的那样,您选择要做的一个取决于您,我个人更喜欢后两个,但这就是我。

这是因为您正在访问static function内部的non static field 就你而言。

ArrayList<Student> rayList = new ArrayList<Student>(); // declare instance variable.

而且,您无法访问没有创建类实例的实例变量。

改用

public static void main(String[] args) {
    ArrayList<Student> rayList = new ArrayList<Student>(); // creates local instance of rayList
    rayList.add(new Student("Sam", 17));
    rayList.add(new Student("Sandra", 18));
    rayList.add(new Student("Billy", 16));
    rayList.add(new Student("Greg", 17));
    rayList.add(new Student("Jill", 18));

    System.out.println(rayList.get(0));
}

该错误意味着rayList不是静态的(换句话说,它是Student类的成员),但是您的main方法是:

/** THIS IS NOT STATIC **/
ArrayList<Student> rayList = new ArrayList<Student>();
...

public static void main(String[] args) {
    /** THIS SCOPE IS STATIC **/
    System.out.println(rayList.get(0));
}

实际上,你不能让列表Student S作为的成员, Student ,因为你要么得到一个堆栈溢出或耗尽内存,这取决于你如何创建它,所以我猜你想rayList是静态的。 那将使rayList不再是Student的成员。

您也不能以您在初始化块或方法之外的方式添加到ArrayList

有关初始化程序块的更多信息,请参见此处:

或者,您可以将引用rayList所有内容rayList main方法中。

rayList变量设置为static可以在main方法中使用它,如下所示:

然后在main方法中添加对象并使用它:

static ArrayList<Student> rayList = new ArrayList<Student>();
public static void main(String[] arg){

    rayList.add(new Student("Sam", 17));
    rayList.add(new Student("Sandra", 18));
    rayList.add(new Student("Billy", 16));
    rayList.add(new Student("Greg", 17));
    rayList.add(new Student("Jill", 18));

    //Do whatever you want.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM