Java 为每个用户创建或建立对象的ArrayList
好的,我正在为一所大学建立一个在线注册系统。这是一个用java编写的相当基本的系统,因此不需要担心数据库问题。我的问题是:我有一类叫做“课程”的对象。每门课程都有一个属性列表(id、时间、讲师等)。然后,每个用户都有一个arraylist(如果愿意,也可以是schedule)对象,他们可以添加或删除这些对象。我的问题是如何为每个学生/用户创建arraylist?有一个单独的课程列表(如目录)可供选择,这是否有益?关于这个问题的任何建议都会有所帮助。如果你想看到我的代码到目前为止的例子,让我知道,我会编辑我的文章,包括它Java 为每个用户创建或建立对象的ArrayList,java,arraylist,Java,Arraylist,好的,我正在为一所大学建立一个在线注册系统。这是一个用java编写的相当基本的系统,因此不需要担心数据库问题。我的问题是:我有一类叫做“课程”的对象。每门课程都有一个属性列表(id、时间、讲师等)。然后,每个用户都有一个arraylist(如果愿意,也可以是schedule)对象,他们可以添加或删除这些对象。我的问题是如何为每个学生/用户创建arraylist?有一个单独的课程列表(如目录)可供选择,这是否有益?关于这个问题的任何建议都会有所帮助。如果你想看到我的代码到目前为止的例子,让我知道,
public class Course {
private int courseId;
private String courseDes;
private String courseIns;
private int time;
public Course(int courseId, String courseDes, String courseIns, int time) {
courseId = this.courseId;
courseDes = this.courseDes;
courseIns = this.courseIns;
time = this.time;
}
无需使用地图;您自己表达了正确的关系:“每个用户都有一个
ArrayList
”。表示has-a关系的方法是使用实例字段:
public class Student {
private final List<Course> courses = new ArrayList<>();
//write methods that operate on courses, or make courses public
....
公共班级学生{
私人最终列表课程=新建ArrayList();
//编写对课程进行操作或公开课程的方法
....
如果您以任何方式关心课程的属性,那么将课程表示为课程对象是最简单的。但是,如果您只需要知道课程ID,或者如果您需要存储大量学生,您可以通过将课程存储为整数或短字符并在静态表中查找它们来节省空间。我有三个sepa对班级、课程、学生和入学情况进行评分
public class Course {
private int courseId;
private String courseDes;
private String courseIns;
private int time;
public Course(int courseId, String courseDes, String courseIns, int time) {
courseId = this.courseId;
courseDes = this.courseDes;
courseIns = this.courseIns;
time = this.time;
}
}
学生
public class Student {
private final int studentID;
private final String name;
private Set<Course> studentCourses;
public Student(int studentId, String name) {
this.name = name;
this.studentID = studentId;
}
public String getName(){
return this.name;
}
public int getStudentId(){
return this.studentID;
}
void addCourse(Course course) {
if(!studentCourses.contains(course)){
studentCourses.add(course);
}
else{
studentCourses.remove(course);
studentCourses.add(course);
}
}
@Override
public int hashCode() {
int hash = 7;
hash = 23 * hash + this.studentID;
hash = 23 * hash + (this.name != null ? this.name.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Student other = (Student) obj;
if (this.studentID != other.studentID) {
return false;
}
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
return true;
}
}
公共班级学生{
私立期末国际学生;
私有最终字符串名;
私人设置的学生课程;
公立学生(int studentId,字符串名称){
this.name=名称;
this.studentID=studentID;
}
公共字符串getName(){
返回此.name;
}
public int getStudentId(){
返回此.studentID;
}
无效添加课程(课程){
如果(!studentCourses.contains(课程)){
学生课程。添加(课程);
}
否则{
学生课程。删除(课程);
学生课程。添加(课程);
}
}
@凌驾
公共int hashCode(){
int hash=7;
hash=23*hash+this.studentID;
hash=23*hash+(this.name!=null?this.name.hashCode():0);
返回散列;
}
@凌驾
公共布尔等于(对象obj){
if(obj==null){
返回false;
}
如果(getClass()!=obj.getClass()){
返回false;
}
最终学生其他=(学生)obj;
if(this.studentID!=其他.studentID){
返回false;
}
如果((this.name==null)?(other.name!=null):!this.name.equals(other.name)){
返回false;
}
返回true;
}
}
招生
class Enrollment{
//This Map will group student with the same name
private Map<String, List<Student>> enrollment;
public Enrollment(Student student){
if(enrollment.containsKey(student.getName())){
enrollment.get(student.getName()).add(student);
}else
{
List<Student> newStudent = new ArrayList<Student>();
newStudent.add(student);
enrollment.put(student.getName(), newStudent);
}
}
public void addCourse(Student student, Course course){
try{
List<Student> studentSameName = enrollment.get(student.name);
for(Student studentEntry : studentSameName){
if(studentEntry.getStudentId() == student.getStudentId()){
studentEntry.addCourse(course);
}
}
}catch(NullPointerException e){
//student does not exist
//TODO Add Logic
}
}
public void removeStudent(Student student){
//TODO Add Logic
}
}
班级注册{
//此地图将对同名学生进行分组
私人地图注册;
公开招生(学生){
if(registration.containsKey(student.getName()){
registration.get(student.getName()).add(student);
}否则
{
List newStudent=new ArrayList();
newStudent.add(学生);
registration.put(student.getName(),newStudent);
}
}
公共课程(学生,课程){
试一试{
List studentSameName=registration.get(student.name);
for(学生姓名:studentSameName){
if(studentry.getStudentId()==student.getStudentId()){
学生中心。添加课程(课程);
}
}
}捕获(NullPointerException e){
//学生不存在
//TODO添加逻辑
}
}
公共无效撤销学生(学生){
//TODO添加逻辑
}
}
最基本的方法是拥有一个映射
,其中用户标识符
是您用来识别特定用户的某种类型。您能提供您的代码并回答这个问题吗?我不熟悉映射界面,这会做什么?从:一个将键映射到值的对象。
在thinki之后有一段时间我一直在思考这个问题,我得出了相同的结论。但是,学生类中的arraylist不应该是公共的吗?或者为了封装起见,我们应该将它们保持为私有的。@QuailMan如果您希望学生接受课程
的任何组合,它可以是公共的;但是实际上您可能希望控制课程的访问权限
确保学生有合理的时间表,例如按时间顺序排列课程,不上两次同一门课,等等。