简体   繁体   English

如何将元素添加到数组列表中?

[英]How do I add my elements to my arraylist?

public class Student {
    private String name;
    private String id;
    private String email;
    private  ArrayList<Student> s;

    public Student( String n, String i, String e)
    {
        n = name; i= id; e = email;
    }
}


public class Library {

    private  ArrayList<Student> s;

    public void addStudent(Student a)
    {
        s.add(a);
    }


    public static void main(String[] args)
    {
        Student g = new Student("John Elway", "je223", "j@gmail.com");
        Student f = new Student("Emily Harris", "emmy65", "e@yahoo.com");
        Student t = new Student("Sam Knight", "joookok", "fgdfgd@yahoo.com");

        s.addStudent(g);
        s.addStudent(f);
        s.addStudent(t);
    }

} }

It seems like that my student objects would be added to the Student s arraylist, but it is not working that way. 看来我的学生对象将被添加到学生的数组列表中,但这种方式无法正常工作。 Is it not working due to the arraylist being in the library class instead of the Student class? 由于arraylist在库类而不是Student类中而导致工作不正常吗?

Shouldn't be your constructor like this? 不应该是这样的构造函数吗?

public Student( String n, String i, String e)
{
    name = n; id = i; email = e;
}

There are a couple of problems with your code: 您的代码有两个问题:

  1. main is a static method, meaning that it executes outside the context of any instance of Library . main是一个static方法,意味着它在任何Library实例的上下文之外执行。 However, s is an instance field of Library . 但是, sLibrary的实例字段。 You should either make s a static field or create an instance of Library and reference the field through that instance: 您应该让s一个static字段或创建的实例Library ,并通过实例引用领域:

     public static void main(String[] args) { Library lib = new Library(); . . . lib.addStudent(g); // etc. } 
  2. addStudent is not a member function of ArrayList ; addStudent不是ArrayList的成员函数; it is a member function of Library . 它是Library的成员函数。 Thus, you should not be coding s.addStudent(f); 因此,您不应编码s.addStudent(f); , etc.

  3. You don't initialize s , so the first time your code tries to add an element, you will get a NullPointerException . 您无需初始化s ,因此您的代码第一次尝试添加元素时,将获得NullPointerException You should initialize it either inline: 您应该内联初始化它:

     private ArrayList<Student> s = new ArrayList<Student>(); 

    or write a constructor for Library and initialize the field there. 或为Library写一个构造函数并在那里初始化字段。

  4. Your latest change—to add private ArrayList<Student> s; 您的最新更改-添加private ArrayList<Student> s; to the Student class—is on the wrong track. Student班级–走错了路。 You will end up with a separate list of students for each student you create; 最后,您将为创建的每个学生提供单独的学生列表; surely not what you want! 当然不是您想要的! The list of students belongs with the Library where it was. 学生名单属于所在的Library

  5. Your constructor for Student looks like it has the assignments backwards. 您的Student构造函数看起来像是向后分配的。

You're trying to add directly to an instance ArrayList from a static method, something that you can't do, and more importantly, someething that you shouldn't do. 您试图从静态方法直接添加到实例ArrayList,这是您不能做的事情,更重要的是,您不应该做的事情。 You need to first create a Library instance within your main method before you can call methods on it. 您需要先在主方法中创建一个Library实例,然后才能对其调用方法。

Library myLibrary = new Library();
myLibrary.add(new Student("John Elway", "je223", "j@gmail.com"));
// ... etc...
public class Library {

private  ArrayList<Student> s = new ArrayList<Student>(); //you forgot to create ArrayList

public void addStudent(Student a)
{
    s.add(a);
}


public static void main(String[] args)
{
    Student g = new Student("John Elway", "je223", "j@gmail.com");
    Student f = new Student("Emily Harris", "emmy65", "e@yahoo.com");
    Student t = new Student("Sam Knight", "joookok", "fgdfgd@yahoo.com");
    Library  library = new Library ();
    library.addStudent(g);
    library.addStudent(f);
    library.addStudent(t);
}

and change your constructor like this 然后像这样改变你的构造函数

public Student( String n, String i, String e)
{
        this.name = n;
        this.id = i; 
        this.email = e;
}

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

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