利用集合存储课程信息:
1.Course类
package com.collection; public class Course { public String id; public String name; public Course(String id,String name) { this.id=id; this.name=name; } }
2.ListTest类
package com.collection; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; /** * 备选课程类 */ public class ListTest { //List用于存放备选课程 public List CourseToSelect; public ListTest(){ this.CourseToSelect = new ArrayList(); } //用于往CourseToSelect添加备选课程 public void TestAdd(){ //创建一个课程对象,并通过调用add方法,添加到备选课程List中 Course c1 = new Course("1","数据结构"); CourseToSelect.add(c1); Course temp = (Course) CourseToSelect.get(0); System.out.println("添加了课程:"+temp.id+"_"+temp.name); Course c2 = new Course("2","c语言"); CourseToSelect.add(0, c2); Course temp2 = (Course) CourseToSelect.get(0); System.out.println("添加了课程:"+temp2.id+"_"+temp2.name); Course[] course = {new Course("3","离散数学"),new Course("4","汇编语言")}; CourseToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) CourseToSelect.get(2); Course temp4 = (Course) CourseToSelect.get(3); System.out.println("添加了两门课程:"+temp3.id+"_"+temp3.name+";"+temp4.id+"_"+temp4.name); CourseToSelect.add(c1); Course temp0 = (Course) CourseToSelect.get(0); System.out.println("添加了课程:"+temp0.id+"_"+temp0.name); Course[] course2 = {new Course("5","高等数学"),new Course("6","大学英语")}; CourseToSelect.addAll(2, Arrays.asList(course2)); Course temp5 = (Course) CourseToSelect.get(2); Course temp6 = (Course) CourseToSelect.get(3); System.out.println("添加了两门课程:"+temp5.id+"_"+temp5.name+";"+temp6.id+"_"+temp6.name); } //取得List中元素的方法 public void testGet(){ int size = CourseToSelect.size(); System.out.println("有如下课程待选:"); for(int i = 0;i<size;i++) { Course cr = (Course) CourseToSelect.get(i); System.out.println("课程:"+cr.id+":"+cr.name); } } //通过迭代器遍历List中的元素 public void testIterator(){ //通过集合的iterator方法,取得迭代器的实例; Iterator it = CourseToSelect.iterator(); System.out.println("有如下课程待选(通过迭代器取得):"); while(it.hasNext()){ Course cr2 = (Course) it.next(); System.out.println("课程:"+cr2.id+":"+cr2.name); } } //通过for each遍历list中的元素 public void testForEach(){ System.out.println("有如下课程待选(通过for each取得):"); for(Object obj:CourseToSelect){ Course cr = (Course) obj; System.out.println("课程:"+cr.id+":"+cr.name); } } //修改List中的元素 public void testModify(){ CourseToSelect.set(6, new Course("7","毛概")); } //删除List中的元素 public void testRemove(){ /*Course cr = (Course) CourseToSelect.get(6); System.out.println("课程:"+cr.id+":"+cr.name+"我即将被删除"); CourseToSelect.remove(cr); //删除具体的元素*/ //CourseToSelect.remove(4); //删除索引4位置上的元素 Course[] courses = {(Course)CourseToSelect.get(4),(Course)CourseToSelect.get(5)}; CourseToSelect.removeAll(Arrays.asList(courses)); System.out.println("成功被删除"); testForEach(); } public static void main(String[] args){ ListTest lt = new ListTest(); lt.TestAdd(); lt.testGet(); lt.testIterator(); lt.testForEach(); lt.testModify(); lt.testForEach(); lt.testRemove(); } }
3.testGeneric类
package com.collection; import java.util.ArrayList; import java.util.List; public class TestGeneric { /** * @param args */ //带有泛型List--course的类型属性 public List<Course> courses; public TestGeneric(){ this.courses=new ArrayList<Course>(); } //测试添加 public void testAdd(){ Course cr1 = new Course("1","大学语文"); courses.add(cr1); courses.add("平常的字符串"); } public static void main(String[] args) { // TODO 自动生成的方法存根 } }
courses.add("平常的字符串") 编译器报错,因为泛型对规定了类型必须是course类型的元素
泛型例子
1.TestGeneric类
package com.collection; import java.util.ArrayList; import java.util.List; public class TestGeneric { /** * @param args */ //带有泛型List--course的类型属性 public List<Course> courses; public TestGeneric(){ this.courses=new ArrayList<Course>(); } //测试添加 public void testAdd(){ Course cr1 = new Course("1","大学语文"); courses.add(cr1); //泛型集合中,不能添加泛型规定的类型以及子类型以外的对象,否则编译器报错 //courses.add("平常的字符串"); Course cr2 = new Course("2","java基础"); courses.add(cr2); } //泛型集合可以添加泛型子类型的对象实例 public void testChild(){ ChildCourse ccr = new ChildCourse(); ccr.id="3"; ccr.name="我是子类型的课程对象实例 "; courses.add(ccr); } //泛型不能使用基本类型 public void testBasicType(){ List<Integer> list = new ArrayList<Integer>(); list.add(1); System.out.println("基本类型必须使用包装类作为泛型~~"+list.get(0)); } //测试方法 public void testForEach(){ for(Course cr:courses){ System.out.println(cr.id+":"+cr.name); } } public static void main(String[] args) { // TODO 自动生成的方法存根 TestGeneric tg = new TestGeneric(); tg.testAdd(); tg.testForEach(); tg.testChild(); tg.testForEach(); tg.testBasicType(); } }
2.ChildCourse继承Course类(在建立类中选择超类Course中即可)
package com.collection; public class ChildCourse extends Course { }
建立继承类只是为了验证泛型中可以添加子类型的对象实例
学生手动选课,显示学生的选课信息
1.student类
package com.collection; import java.util.HashSet; import java.util.Set; /** * @学生类 * */ public class Student { public String id; public String name; public Set<Course> courses; public Student(String id,String name) { this.id=id; this.name=name; this.courses=new HashSet<Course>(); } }
2.SetTest类
package com.collection; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class SetTest { /** * @param args */ public List<Course> CourseToSelect; public SetTest(){ CourseToSelect = new ArrayList<Course>(); } //添加元素 public void TestAdd(){ //创建一个课程对象,并通过调用add方法,添加到备选课程List中 Course c1 = new Course("1","数据结构"); CourseToSelect.add(c1); Course temp = (Course) CourseToSelect.get(0); //System.out.println("添加了课程:"+temp.id+"_"+temp.name); Course c2 = new Course("2","c语言"); CourseToSelect.add(0, c2); Course temp2 = (Course) CourseToSelect.get(0); //System.out.println("添加了课程:"+temp2.id+"_"+temp2.name); Course[] course = {new Course("3","离散数学"),new Course("4","汇编语言")}; CourseToSelect.addAll(Arrays.asList(course)); Course temp3 = (Course) CourseToSelect.get(2); Course temp4 = (Course) CourseToSelect.get(3); //System.out.println("添加了两门课程:"+temp3.id+"_"+temp3.name+";"+temp4.id+"_"+temp4.name); //CourseToSelect.add(c1); //Course temp0 = (Course) CourseToSelect.get(0); //System.out.println("添加了课程:"+temp0.id+"_"+temp0.name); Course[] course2 = {new Course("5","高等数学"),new Course("6","大学英语")}; CourseToSelect.addAll(2, Arrays.asList(course2)); Course temp5 = (Course) CourseToSelect.get(2); Course temp6 = (Course) CourseToSelect.get(3); //System.out.println("添加了两门课程:"+temp5.id+"_"+temp5.name+";"+temp6.id+"_"+temp6.name); } //测试For Each打印输出 public void testForEach(){ System.out.println("有如下课程待选(通过for each取得):"); for(Object obj:CourseToSelect){ Course cr = (Course) obj; System.out.println("课程:"+cr.id+":"+cr.name); } } public static void main(String[] args) { // TODO 自动生成的方法存根 SetTest st = new SetTest(); st.TestAdd(); st.testForEach(); Student student = new Student("1","小明"); System.out.println("欢迎同学:"+student.name+"选课!"); //创建Scanner对象,用于从键盘中输入课程的ID; Scanner console = new Scanner(System.in); for(int i=0;i<3;i++){ System.out.println("输出课程ID"); String courseId=console.next(); for (Course cr : st.CourseToSelect) { if(cr.id.equals(courseId)){ student.courses.add(cr); } } } st.testForEachForSet(student); } // 打印输出,学生所选的课程 public void testForEachForSet(Student student){ for (Course cr : student.courses) { System.out.println("选择了课程:"+cr.id+":"+cr.name); } } }
注:Set中,添加某个对象,无论添加多少次,最终只会保留一个该对象(的引用),并且,保留的事第一次添加的那个。
添加了两次课程1 但是最后的结果就是选择了两门课程。