【问题标题】:My ArrayList won't recognize what I add to the list我的 ArrayList 无法识别我添加到列表中的内容
【发布时间】:2013-09-27 06:48:46
【问题描述】:

这是代码本身

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 进行静态引用”

【问题讨论】:

    标签: java arraylist


    【解决方案1】:

    您正试图在可执行上下文之外执行代码。代码只能从方法、静态初始化程序或实例初始化程序(感谢 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 是因为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,它没有增加任何价值。你能想象在向List 添加任何实例之前必须创建Student 的实例吗?

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

    制作rayListstatic

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

    正如我所说,你选择做哪一个取决于你,我个人更喜欢最后两个,但那就是我。

    【讨论】:

    • 您已经解决了问题(有 2 个),但只解释了问题的一部分,而不是 OP 询问的问题。不,静态初始化程序和方法不是可以执行代码的唯一上下文。还有实例初始化器。
    • @NickC 第一个感谢实例初始化程序,第二个,你能解释一下“而不是 OP 询问的那个”吗?正如我所看到的,OP 不确定为什么他们的代码会出错,这是因为执行上下文......
    • 在我看来,编译器捕获的第一个错误是rayList 不是静态的,而是从静态方法main 引用的。我认为 OP 也应该知道这一点,但重要的是你也发现了另一个错误。
    • @NickC 感谢您抽出宝贵时间提供反馈,以便我学习新知识并改进我的答案和您的建议
    【解决方案2】:

    该错误意味着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));
    }
    

    实际上,您无法将Students 列为Student 的成员,因为您将获得堆栈溢出或内存不足,具体取决于您如何创建它,所以我猜你希望rayList 是静态的。这将使rayList 不再是Student 的成员。

    您也不能像在初始化程序块或方法之外那样添加到您的ArrayList

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

    或者您可以在您的 main 方法中移动所有引用 rayList 的内容。

    【讨论】:

      【解决方案3】:

      这是因为您在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));
      }
      

      【讨论】:

        【解决方案4】:

        rayList 变量设为静态,以便在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.
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-24
          • 2020-03-16
          • 1970-01-01
          • 1970-01-01
          • 2018-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多