随着一点点研究,你可以找到很多教程,以达到你想要什么,但我会尝试设置你在正确的道路只是让你有一个answear并在某处开始。
什么是学生? 它是一个只包含一个名称的字符串,它是一个表示一个可以拥有一些属性的学生的对象吗? 一个例子是
public class Student{
private int number;
private String name;
private int age;
// Basically anything that makes sense for a student.
public Student(int number, String name, int age){
this.number = number;
this.name = name;
this.age = age;
}
// .... Getters and Setters.
}
你需要一些地方来存储每一个学生加入到课程,那就是ArrayList的是什么,即
List<Student> students = new ArrayList<Student>();
Student foo = new Student(23, "Foo", 22);
students.add(foo); // This is how you add to a List (in this case a List of Student objects and more precisely an ArrayList of Students).
您将需要保持列表在你的课程中作为一个实例变量,并添加一个方法,你可以传递一个学生,并在方法内部添加你的学生列表,如果你愿意,你甚至可以做一些验证。
如果您在提出可轻松找到的问题之前先有更多疑问,请先搜索解决方案。 这里有一些参考:
Java List
Java ArrayList
编辑要添加你的学生几乎做的方式,但你有一个错误,也是你的学生名单只驻留在构造函数中,这意味着你不能使用这个列表来保存学生以外的东西。
下面是正确的代码
/**
* Constructor with parameters
* @param noOfStudents integer
* @param courseName String with the Course name
* @param teacher String with the teacher
*/
public Course(int noOfStudents, String courseName, String teacher){
this.studentList = new ArrayList<Student>(); // The declaration is in above in your class, as an instance variable.
this.courseName = courseName;
this.teacher = teacher;
}
ArrayList<Student> studentList; // You can move this so it sits above besides your other variables, but it will also work like this.
public boolean addStudent(Student student){
if (student==null || studentList.contains(student)) { // You had Student.contains, wich will give an error because Student (class) doesnt have a static method named contains.
return false;
}
studentList.add(student); // you had the same problem here, you had Student.add(student), wich is wrong and it would not compile.
return true;
}
确保您已经创建了Student类,它是没有任何错误。
测试和运行的代码,将其更改为更精确地fullfill您的需求
import java.util.ArrayList;
public class Course {
private String courseName;
private int noOfStudents;
private String teacher;
public static int instances = 0;
private ArrayList<Student> studentList;
//Getters
public String getCourseName(){
return this.courseName;
}
public int getNoOfStudents(){
return this.noOfStudents;
}
public String getTeacher(){
return this.teacher;
}
//Setters
public void setCourseName(String courseName){
this.courseName = courseName;
}
public void setNoOfStudents(int noOfStudents){
this.noOfStudents = noOfStudents;
}
public void setTeacher(String teacher){
this.teacher = teacher;
}
/**
* Default constructor. Populates course name, number of students with defaults
*/
public Course(){
instances++;
this.noOfStudents = 0;
this.courseName = "Not Set";
this.teacher = "Not Set";
}
/**
* Constructor with parameters
* @param noOfStudents integer
* @param courseName String with the Course name
* @param teacher String with the teacher
*/
public Course(int noOfStudents, String courseName, String teacher){
this.studentList = new ArrayList<Student>();
this.courseName = courseName;
this.teacher = teacher;
}
public boolean addStudent(Student student){
if (student==null || studentList.contains(student)) {
return false;
}
studentList.add(student);
return true;
}
public void printStudents(){
for(Student s : studentList)
System.out.println(s.getName() + ", with " + s.getAge() + " year(s)");
}
public static class Student{
private int number;
private String name;
private int age;
// Basically anything that makes sense for a student.
public Student(int number, String name, int age){
this.number = number;
this.name = name;
this.age = age;
}
// .... Getters and Setters.
public int getNumber(){
return this.number;
}
public String getName(){
return this.name;
}
public int getAge(){
return this.age;
}
}
// Testing code
public static void main(String[] args){
Course oop = new Course(6, "Object Oriented Programming", "LeBron James");
oop.addStudent(new Course.Student(6, "Michael Jordan", 56));
oop.addStudent(new Course.Student(23, "Kyrie Irving", 24));
oop.addStudent(new Course.Student(14, "Kevin Love", 27));
System.out.println(oop.getCourseName() + " has the following students");
oop.printStudents();
}
}
“我现在不知道该如何实现数组列表。”您不需要实现ArrayList。这是一个内置的集合。声明如下:列表 students = new ArrayList ();'。假设你实际上已经创建了Student类。 –
Arqan
您是否阅读过文档和/或查看过关于'ArrayList'的任何教程/指南? – tnw
@Arqan我该在哪里申报? – donk2017