简体   繁体   English

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

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

Here is the code itself 这是代码本身

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));
    }

}

Some println commands are missing in the main method. main方法中缺少某些println命令。 But when I try to add the 5 students to my ArrayList, I get the error "Cannot make a static reference to the non-static field rayList" 但是,当我尝试将5个学生添加到ArrayList时,出现错误“无法对非静态字段rayList进行静态引用”

You're trying to execute code outside of executable context. 您正在尝试在可执行上下文之外执行代码。 Code can only be executed from a method, static initialiser or instance initialiser (Thanks NickC) context. 只能从方法,静态初始化程序或实例初始化程序(感谢NickC)上下文中执行代码。

Try moving it into the main method to start with... 尝试将其移入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));
}

Updated based on feedback 根据反馈进行了更新

Your first error Cannot make a static reference to the non-static field rayList was generated because the rayList was not declared static , but you were trying to reference it from a static context. 您的第一个错误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 is declared as a "instance" field/variable, meaning that it requires an instance of the declaring class ( Student ) before it has meaning. rayList被声明为“实例”字段/变量,这意味着它需要声明类( Student )的实例才有意义。

This could be resolved by... 这可以通过...解决

Creating an instance of Student in main and accessing it via that instance, for example... main创建一个Student实例,然后通过该实例访问它,例如...

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

Personally, I don't like this, rayList doesn't really belong with Student , it adds no value. 我个人不喜欢这样, rayList并不真正属于Student ,它没有任何价值。 Can you imagine having to create an instance of Student before you add any to the List ? 您能想象在将任何Student添加到List之前必须创建Student实例吗?

I also don't like accessing instance fields directly, but that's a personal preference. 我也不喜欢直接访问实例字段,但这是个人喜好。

Making rayList static 使rayList static

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

This is a viable option, but would require more context before it could be deemed good or bad. 这是一个可行的选择,但是在被认为是好是坏之前,需要更多的上下文。 I personally feel that static fields can cause more problems than they solve, but again, that's a personal opinion and your context may deem a reasonable solution. 我个人认为, static字段可能会导致更多问题,而不是解决的问题,但这再次是个人观点,您的上下文可能会认为是合理的解决方案。

Or, you could create a local instance of the List within the context of the static method as seen in the first example. 或者,您可以在static方法的上下文中创建List的本地实例,如第一个示例所示。

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

As I said, which one you choice to do will come down to you, I would personally prefer the last two, but that's me. 就像我说的那样,您选择要做的一个取决于您,我个人更喜欢后两个,但这就是我。

This is because you are accessing non static field inside static function . 这是因为您正在访问static function内部的non static field In your case. 就你而言。

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

And you cannot access instance variable w/o creating instance of a class. 而且,您无法访问没有创建类实例的实例变量。

Instead use, 改用

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));
}

That error means that rayList is not static (in other words, it is a member of class Student ) but your main method is: 该错误意味着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));
}

You actually can't make a list of Student s as a member of Student because you will either get a stack overflow or run out of memory, depending on how you create it, so I'm guessing you wanted rayList to be static. 实际上,你不能让列表Student S作为的成员, Student ,因为你要么得到一个堆栈溢出或耗尽内存,这取决于你如何创建它,所以我猜你想rayList是静态的。 That would make rayList no longer a member of Student . 那将使rayList不再是Student的成员。

You also cannot add to your ArrayList the way you are outside of an initializer block or method. 您也不能以您在初始化块或方法之外的方式添加到ArrayList

See here for more on initializer blocks: 有关初始化程序块的更多信息,请参见此处:

Or you could move everything referring to rayList inside your main method. 或者,您可以将引用rayList所有内容rayList main方法中。

Make the rayList variable static to use it in main method like this : rayList变量设置为static可以在main方法中使用它,如下所示:

Then add the objects in main method and use it : 然后在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