Java 如何将元素添加到arraylist? 公共班级学生{ 私有字符串名称; 私有字符串id; 私人字符串电子邮件; 私人ArrayList s; 公立学生(字符串n、字符串i、字符串e) { n=姓名;i=id;e=电子邮件; } } 公共班级图书馆{ 私人ArrayList s; 公立学校学生(学生a) { s、 添加(a); } 公共静态void main(字符串[]args) { 学生g=新学生(“约翰·埃尔韦”、“je223”、”j@gmail.com"); 学生f=新学生(“艾米丽·哈里斯”、“艾米丽65”e@yahoo.com"); 学生t=新学生(“山姆·奈特”、“朱考克”fgdfgd@yahoo.com"); s、 addStudent(g); s、 学生(f); s、 addStudent(t); }
}Java 如何将元素添加到arraylist? 公共班级学生{ 私有字符串名称; 私有字符串id; 私人字符串电子邮件; 私人ArrayList s; 公立学生(字符串n、字符串i、字符串e) { n=姓名;i=id;e=电子邮件; } } 公共班级图书馆{ 私人ArrayList s; 公立学校学生(学生a) { s、 添加(a); } 公共静态void main(字符串[]args) { 学生g=新学生(“约翰·埃尔韦”、“je223”、”j@gmail.com"); 学生f=新学生(“艾米丽·哈里斯”、“艾米丽65”e@yahoo.com"); 学生t=新学生(“山姆·奈特”、“朱考克”fgdfgd@yahoo.com"); s、 addStudent(g); s、 学生(f); s、 addStudent(t); },java,arraylist,Java,Arraylist,} 看起来我的student对象将被添加到student的arraylist中,但它不是这样工作的。由于arraylist位于library类而不是Student类中,它是否不起作用?您试图从静态方法直接添加到实例arraylist,这是您不能做的,更重要的是,这是您不应该做的。您需要首先在主方法中创建一个库实例,然后才能对其调用方法 public class Student { private String name; private String id; privat
看起来我的student对象将被添加到student的arraylist中,但它不是这样工作的。由于arraylist位于library类而不是Student类中,它是否不起作用?您试图从静态方法直接添加到实例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);
}
你的构造器不应该是这样的吗
Library myLibrary = new Library();
myLibrary.add(new Student("John Elway", "je223", "j@gmail.com"));
// ... etc...
您的代码有几个问题:
main
是一种static
方法,意味着它在库的任何实例的上下文之外执行。但是,s
是Library
的一个实例字段。您应该将s
设置为静态
字段,或者创建库的实例
并通过该实例引用该字段:
public Student( String n, String i, String e)
{
this.name = n;
this.id = i;
this.email = e;
}
addStudent
不是ArrayList
的成员函数;它是库
的成员函数。因此,您不应该编写s.addStudent(f)代码>,等等
s
,因此当您的代码第一次尝试添加元素时,您将得到一个NullPointerException
。您应该以内联方式初始化它:
public static void main(String[] args) {
Library lib = new Library();
. . .
lib.addStudent(g);
// etc.
}
private ArrayList s=new ArrayList();
或者为库
编写一个构造函数,并在那里初始化字段private ArrayList s的更改向学生
班级发送的代码在错误的轨道上。您将为您创建的每个学生创建一个单独的学生列表;当然不是你想要的!学生名单属于图书馆
Student
的构造函数看起来像是将作业向后分配你试过编译这个吗
s
与您的main
方法处于不同的上下文中。您需要更频繁地测试代码并在尝试创建更多代码之前修复问题,否则您只会遇到一堆问题(如上文所示).@SotiriosDelimanolis-这正是OP说他需要帮助的问题。@TedHopp我读到它不工作是因为arraylist在library类而不是学生类?不,那与此无关。您有一个静态上下文试图访问实例上下文。@SotiriosDelimanolis-我的意思是,OP似乎已经在报告编译器错误。igknighton——如果您发布了实际的编译器错误消息,这会有所帮助。1+和很好的提示。igknighton的代码似乎有很多错误。至少请去掉代码中的s.addStudent(…)
垃圾。
public Student( String n, String i, String e)
{
this.name = n;
this.id = i;
this.email = e;
}
public static void main(String[] args) {
Library lib = new Library();
. . .
lib.addStudent(g);
// etc.
}
private ArrayList<Student> s = new ArrayList<Student>();