这是作业:
一个学生实体除具有一个大学ID 之外,还具有一个名称和地址(均由一个 class Name 和 Address 的对象表示),和以课程ArrayList表示的课程表
因此,我正在考虑使用ArrayList来保存学生列表,并在创建新学生之前检查该学生是否存在。抱歉,这是我的第一个问题,所以我正在尽力解释一下:
这是我的地址课程:
public class Address {
private int streetNumber;
private String streetName;
private String city;
private String state;
private int province;
private String country;
public Address (int streetNumber,String streetName,String city,String state,int province,String country)
{
this.streetNumber=streetNumber;
this.streetName=streetName;
this.city=city;
this.state=state;
this.province=province;
this.country=country;
}
public int getStreetNumber() {
return streetNumber;
}
public void setStreetNumber(int streetNumber) {
this.streetNumber = streetNumber;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getProvince() {
return province;
}
public void setProvince(int province) {
this.province = province;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String toString() {
return " [streetNumber=" + streetNumber + ", streetName=" + streetName
+ ", city=" + city + ", state=" + state + ", province="+province+", country="
+ country + "]";
}
public boolean equals(Address add)
{
if(add==null)
{
return true;
}
if(this.getClass()!=add.getClass())
{
return false;
}
Address address=(Address) add;
return streetNumber==address.streetNumber &&
province==address.province && streetName.equals(address.streetName)
&& city.equals(address.city)&& state.equals(address.state)&& country.equals(address.country);
}
}
这是我的姓名课程
public class Name {
private String firstName;
private String lastName;
private char middle;
public Name (String fiName,String laName, char middle)
{
this.firstName=fiName;
this.lastName=laName;
this.middle=middle;
}
public String getFirst()
{
return firstName;
}
public void setFirst(String first)
{
firstName=first;
}
public String getLast()
{
return lastName;
}
public void setLast(String last)
{
lastName=last;
}
public char getMiddle()
{
return middle;
}
public void setMiddle(char midd)
{
middle=midd;
}
/*public String toString()
{
return "[First Name= "+ firstName +" Last Name "+ lastName+" Middle Name "+ middle +"";
}*/
}
这是我的学生班:
public class Student {
private int studentId;
private Name name;
private Address address;
boolean a;
ArrayList<Course> courseSchedule = new ArrayList<Course>();
ArrayList<Student> student=new ArrayList<Student>();
public Student(String fiName,String laName, char middle,int stNumber,String stName,String city,String state,int province,String country,int id)
{
if(student.contains(id))
{
System.out.println("Student cannot be same id");
}
else
{
address= new Address(stNumber,stName,city,state,province,country);
name=new Name(fiName,laName,middle);
this.studentId=id;
student.add();
}
}
public int getID()
{
return studentId;
}
public void setId(int id)
{
this.studentId = id;
}
public ArrayList<Course> getCourseSchedule()
{
return courseSchedule;
}
public void setCourseSchedule(ArrayList<Course> courseSchedule)
{
this.courseSchedule = courseSchedule;
}
public void addCourse(Course c) {
courseSchedule.add(c);
}
public void dropCourse(Course course) {
courseSchedule.remove(course);
}
}
我的问题是如何将Student对象添加到Student ArrayList中 以及如何使用contains()方法检查ArrayList中是否存在学生ID
student.contains(id)这行在这里似乎不正确 我希望我现在能清楚地解释我的问题。我的英语也很抱歉。
答案 0 :(得分:1)
您不会在Student
的类中保留Student
个对象的列表。您的ArrayList<Student> student=new ArrayList<Student>();
不属于那里。
您将在别处保存另一个名为StudentBody
的结构或集合。实例化学生后,会将其添加到StudentBody
集合中。
List< Student > studentBody = new ArrayList< Student >() ; // This list is stored somewhere else in your app.
您可以在Student
对象中循环List
个StudentBody
对象。对于每个用户,您都将访问UniversityId
成员字段并将其与要添加的新成员进行比较。
或者您可以使用Map
,其中key是UniversityId
对象,而值是Student
对象。添加前检查现有密钥。
这些解决方案忽略了concurrency的重要问题。但这对于编程入门课程中的家庭作业来说可能还可以。
答案 1 :(得分:1)
使用HashMap()
来基于唯一ID收集信息。
public class Student {
private int studentId;
private Name name;
private Address address;
private static HashMap<Integer,Student> students = new ConcurrentHashMap<>(); // Make a static Map so all objectrs shared same data
public Student(String fiName,String laName, char middle,int stNumber,String stName,String city,String state,int province,String country,int id)
{
if(students.contains(id))
{
System.out.println("Student can be same id");
}
else
{
address= new Address(stNumber,stName,city,state,province,country);
name=new Name(fiName,laName,middle);
this.studentId=id;
students.put(id,this); // use this to add current object
}
}
答案 2 :(得分:0)
通过阅读ArrayList源代码,您应该覆盖学生类的euqals函数。
this