创建学生数组列表以将学生添加到课程 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42373289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a student arraylist to add students to course class Java
提问by donk2017
Im trying to create a student arraylist to a a course class so that when a student is added the arraylist is increases. this is the code I have so far:
我试图为课程类创建一个学生数组列表,以便在添加学生时数组列表会增加。这是我到目前为止的代码:
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;
}
}
Im unsure how to implement the array list. Can someone point me in the right direction.
我不确定如何实现数组列表。有人可以指出我正确的方向。
回答by Nayriva
Just add attribute to your class
只需将属性添加到您的类
List<Student> students;
In constructors, initialize this list:
在构造函数中,初始化这个列表:
students = new ArrayList<>();
Create method to add student to list:
创建将学生添加到列表的方法:
public boolean addStudent(Student stud) {
if (stud == null || students.contains(stud)) {
return false;
}
students.add(stud);
return true;
}
Also check https://docs.oracle.com/javase/8/docs/api/java/util/List.htmlfor documentation of list. Question is, do you want to add students in constructor? If so, add parameter to your constructor
另请查看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);
}
回答by Edu G
With a little bit of research you can find many tutorials to achieve what you intend, but i'll try to set you in the right path just so you have an answear and somewhere to start.
通过一些研究,您可以找到许多教程来实现您的意图,但我会尝试为您设置正确的道路,以便您有一个答案和开始的地方。
What is a Student ? Is it a String containing just a name, is it an object that represents a student that can have some properties ? One example is
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. }
You need some place to store every Student added to the course, that is what the ArrayList is for i.e
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).
什么是学生?它是一个只包含一个名字的字符串,它是一个代表可以具有某些属性的学生的对象吗?一个例子是
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).
You will need to keep the list in your course class as an instance variable, and add a method in wich you can pass a student and inside the method all you have to is add to your list of students, you may even do some validation if you want.
您需要将课程列表中的列表作为实例变量保留,并添加一个方法,您可以通过该方法传递学生,在该方法中,您只需将列表添加到学生列表中,您甚至可以进行一些验证,如果你要。
If you have more doubts first search for a solution before asking questions that can easily be found. Here are some references:
如果您有更多疑问,请先搜索解决方案,然后再提出容易找到的问题。以下是一些参考:
EDITthe way you are adding your students is almost done but you have an error and also your list of students only resides inside the constructor, wich means you cannot use the list for saving students outside.
编辑您添加学生的方式几乎完成,但您有一个错误,而且您的学生列表仅位于构造函数内,这意味着您不能使用该列表将学生保存在外面。
Below is the correct code
下面是正确的代码
/**
* 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;
}
Be sure that you have created the Student class and it is without any errors.
确保您已经创建了 Student 类并且它没有任何错误。
Tested and working code, change it to fullfill your needs more precisely
经过测试和工作的代码,更改它以更准确地满足您的需求
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();
}
}