简体   繁体   中英

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?

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 . However, s is an instance field of Library . You should either make s a static field or create an instance of Library and reference the field through that instance:

     public static void main(String[] args) { Library lib = new Library(); . . . lib.addStudent(g); // etc. } 
  2. addStudent is not a member function of ArrayList ; it is a member function of Library . Thus, you should not be coding 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 . You should initialize it either inline:

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

    or write a constructor for Library and initialize the field there.

  4. Your latest change—to add private ArrayList<Student> s; to the Student class—is on the wrong track. 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.

  5. Your constructor for Student looks like it has the assignments backwards.

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. You need to first create a Library instance within your main method before you can call methods on it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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