2017-02-21 115 views
-4

我試圖創建一個學生數組列表,以便當學生被添加時,數組列表增加。這是我到目前爲止的代碼:創建學生數組列表以將學生添加到課程班Java

import java.util.ArrayList; 

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

/** 
* 
* @author Saj 
*/ 
public class Course { 
    private String courseName; 
    private int noOfStudents; 
    private String teacher; 
    public static int instances = 0; 

    //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.noOfStudents = noOfStudents; 
     this.courseName = courseName; 
     this.teacher = teacher; 
    } 

} 

不確定如何實現數組列表。有人能指引我走向正確的方向嗎?

+0

「我現在不知道該如何實現數組列表。」您不需要實現ArrayList。這是一個內置的集合。聲明如下:列表 students = new ArrayList ();'。假設你實際上已經創建了Student類。 – Arqan

+3

您是否閱讀過文檔和/或查看過關於'ArrayList'的任何教程/指南? – tnw

+0

@Arqan我該在哪裏申報? – donk2017

回答

0

只是爲了你的類

List<Student> students; 

添加屬性在構造函數初始化這個列表:

students = new ArrayList<>(); 

創建方法添加學生名單:

public boolean addStudent(Student stud) { 
    if (stud == null || students.contains(stud)) { 
     return false; 
    } 
    students.add(stud); 
    return true; 
} 

還檢查https://docs.oracle.com/javase/8/docs/api/java/util/List.html爲列表文件。 問題是,你想在構造函數中添加學生嗎?如果是這樣,添加參數構造函數

public Course(int noOfStudents, String courseName, 
       String teacher, List<Student> students){ 
    this.noOfStudents = noOfStudents; 
    this.courseName = courseName; 
    this.teacher = teacher; 
    this.students = new Arraylist<>(students); 
} 
+0

查看我發佈的修改過的代碼的答案,但發現錯誤。 – donk2017

+0

你會得到什麼錯誤? 你在哪裏聲明瞭列表屬性? 增加它的權利後,'公共靜態INT實例= 0;' 只需鍵入私人'名單學生;' +一個提示,不要使用 'ArrayList的 studentList =新的ArrayList ();' 使用'列表 studentList = new ArrayList ();'而不是 – Nayriva

1

隨着一點點研究,你可以找到很多教程,以達到你想要什麼,但我會嘗試設置你在正確的道路只是讓你有一個answear並在某處開始。

  1. 什麼是學生? 它是一個只包含一個名稱的字符串,它是一個表示一個可以擁有一些屬性的學生的對象嗎? 一個例子是

    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. 
    } 
    
  2. 你需要一些地方來存儲每一個學生加入到課程,那就是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(); 

    } 

} 
+0

請參閱我用已調整的代碼發佈的答案,但發現錯誤。 – donk2017

+0

@ donk2017看到我的編輯,我用你的代碼糾正了一些事情,你有一些嚴重的問題。 –

+0

我必須使用toString,然後從main類中執行'System.out.println(course.toString());'''和'System.out.println(student.toString());'以打印出課程並學生信息。當代碼運行時,我還必須從終端輸入學生的詳細信息。 – donk2017