public boolean equals(Object obj){
if(this == obj){
return true;
}
if(obj == null){
return false;
}
if(!(obj instanceof Course)){
return false;
}
Course course = (Course)obj;
if(this.name == null){
if(course.name== null){
return true;
}
else{
return false;
}
}else{
if(this.name.equals(course.name)){
return true;
}
else{
return false;
}
}
}
}
public void testAdd(){
//创建一个课程对象,并通过add方法添加进备选课程List中;
Course cr1 = new Course("1","数据结构");
coursesToSelect.add(cr1);
Course cr2 = new Course("2","C语言");
coursesToSelect.add(0, cr2);
Course[] course = {new Course("3","离散数学 "),new Course("4","汇编语言")};
coursesToSelect.addAll(Arrays.asList(course));
Course[] course2 = {new Course("5","高等数学 "),new Course("6","大学英语")};
coursesToSelect.addAll(2,Arrays.asList(course2));
}
public void testForEach(){
System.out.println("有如下课程待选(通过For each来遍历):");
for(Object obj:coursesToSelect){
Course cr = (Course) obj;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 测试List的contains方法
* */
public void testListContains(){
//取得备选课程的第0个元素
Course course= coursesToSelect.get(0);
//打印输出是否包含course的对象
System.out.println("取得课程:"+course.name);
System.out.println("备选课程中是否包含此课程"+coursesToSelect.contains(course));
System.out.println("请输入课程名称");
String name = console.next();
Course course2 = new Course();
course2.name= name;
System.out.println("取得课程:"+course2.name);
System.out.println("备选课程中是否包含此课程"+coursesToSelect.contains(course2));
}
public static void main(String[] args) {
SetTest st = new SetTest();
st.testAdd();
st.testForEach();
st.testListContains();
}