package imooc_collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class SetTest {
public List<Course>coursesToSelect;
private Scanner console;
public SetTest(){
coursesToSelect=new ArrayList<Course>();
console=new Scanner(System.in);
}
public void TestAdd()
{
Course cr1 =new Course("1","数据结构");
coursesToSelect.add(cr1);
Course temp=(Course) coursesToSelect.get(0);
//System.out.println("添加课程"+temp.id+":"+temp.name);
Course cr2 =new Course("2","c语言");
coursesToSelect.add(0, cr2);
Course temp2=(Course) coursesToSelect.get(0);
//System.out.println("添加课程"+temp2.id+":"+temp2.name);
// coursesToSelect.add(cr1);
// Course temp0=(Course) coursesToSelect.get(2);
// System.out.println("添加课程"+temp0.id+":"+temp0.name);
//
// Course cr3 =new Course("3","语言");
// coursesToSelect.add(3, cr3);
// Course temp3=(Course) coursesToSelect.get(0);
// System.out.println("添加课程"+temp3.id+":"+temp3.name);
Course [] course={new Course("3","离散数学"),new Course("4","大学英语")};
coursesToSelect.addAll(Arrays.asList(course));
Course temp3=(Course) coursesToSelect.get(2);
Course temp4=(Course) coursesToSelect.get(3);
//System.out.println("添加课程"+temp3.id+":"+temp3.name+"\n"+temp4.id+":"+temp.name);
Course [] course2={new Course("5","数学"),new Course("6","结构")};
coursesToSelect.addAll(2, Arrays.asList(course2));
Course temp5=(Course) coursesToSelect.get(2);
Course temp6=(Course) coursesToSelect.get(3);
//System.out.println("添加课程"+temp5.id+":"+temp5.name+"\n"+temp6.id+":"+temp6.name);
}
/**
*
*通过foreach
* @param args
*/
public void TestForeach(){
for(Object obj:coursesToSelect){
System.out.println("有如下待选课程foreach");
Course cr=(Course) obj;
System.out.println("待选课程"+cr.id+":"+cr.name);
}
}
/**
* 测试List的contains 方法
* @param args
*/
public void testListContains(){
//取得0个元素的课程
Course course=coursesToSelect.get(0);
System.out.println("取得课程"+course.name);
System.out.println("是否包含"+course.name+","+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("是否包含"+course2.name+","+coursesToSelect.contains(course2));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SetTest t1=new SetTest();
t1.TestAdd();
t1.testListContains();
//t1.TestForeach();
//学生选课
// Student student=new Student("1","小明");
// System.out.println("欢迎"+student.name+"选课");
// Scanner console=new Scanner(System.in);
//
//
// for(int i=0;i<3;i++){
// System.out.println("输入要选课的id");
// String courseId=console.next();
// for (Course cr : t1.coursesToSelect) {
// if(cr.id.equals(courseId)){
// student.courses.add(cr);
// /**
// * 重复添加无意义
// */
// //student.courses.add(cr);
// //student.courses.add(null);
//
//
// }
//
// }
// }
// t1.testForEachSet(student);
}
public void testForEachSet(Student student)
{
System.out.println("选了多少课程"+student.courses.size());
for (Course cr : student.courses) {
System.out.println("选了什么课程"+cr.id+":"+cr.name);
}
}
}
course2.id = "2";
找到原因了,这里我们调用自动重写的话,因为我们这里只是通过name进行比较,所以不能把equals中的id也加进去。