2017-02-21 114 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