隨着一點點研究,你可以找到很多教程,以達到你想要什麼,但我會嘗試設置你在正確的道路只是讓你有一個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