Course.java
public class Course {
/*
* 课程类
*/
public String id;
public String name;
public Course(String id, String name) {
// TODO Auto-generated constructor stub
this.id = id;
this.name = name;
}
}
ListTest.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class ListTest {
/*
* 备选课程
* 如果输入的不是course类型则有异常
*/
private List coursesToSelect;
//List是一个容器 存放备选课程
public ListTest(){//构造方法
this.coursesToSelect = new ArrayList();//ArrayList是List的实现方法
}
/*
* 用于向coursesToSelect中添加备选课程
*/
public void TestAdd(){
//创建一个课程对象,并通过add
Course cr1 = new Course("1","数据结构");
coursesToSelect.add(cr1);
//对象存入集合都变成Object类型,取出时需要类型强转
Course temp1 = (Course) coursesToSelect.get(0);
//确定课程是否添加进去
System.out.println("添加了课程"+temp1.id+":"+temp1.name);
Course cr2 = new Course("2","C语言");
coursesToSelect.add(0, cr2);
Course temp2 = (Course) coursesToSelect.get(0);//0是数组的下标,不能超出数组的长度,一旦超过就有异常
System.out.println("添加了课程"+temp2.id+":"+temp2.name);
//以下方法会抛出数组下标越界异常
//Course cr3 = new Course("3","C语言");
//coursesToSelect.add(4, cr3);
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+"+"+temp4.id+":"+temp4.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+"+"+temp6.id+":"+temp6.name);
coursesToSelect.add(cr1);
Course temp0 = (Course) coursesToSelect.get(2);
System.out.println("添加了课程"+temp1.id+":"+temp1.name);
}
/*
* 取得List中元素 的方法
*/
public void testGet(){
int size = coursesToSelect.size();
System.out.println("有如下课程待选:");
for(int i = 0;i<size;i++){
Course cr = (Course) coursesToSelect.get(i);
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 通过迭代器来遍历List
*/
public void testIterator(){
//通过集合的Iterator方法取得迭代器的实例
//迭代器依附于某个集合存在,并不能单独存在
Iterator it = coursesToSelect.iterator();
System.out.println("有如下课程待选(通过迭代器访问):");
while(it.hasNext()){
Course cr = (Course) it.next();
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 通过for each方法访问集合元素
*/
public void testForEach(){
System.out.println("有如下课程待选(通过ForEach访问):");
for(Object obj:coursesToSelect){
Course cr = (Course) obj;
System.out.println("课程:"+cr.id+":"+cr.name);
}
}
/*
* 修改List中的元素
*/
public void testModify(){
coursesToSelect.set(4,new Course("7","毛概"));
}
/*
* 删除List中的元素
*/
public void testRemove(){
Course cr = (Course) coursesToSelect.get(4);
System.out.println("课程"+cr.id+cr.name+"删除");
System.out.println("成功删除元素");
testForEach();
/*
* 删除多个元素的方法
*
*/
Course[] courses = {(Course)coursesToSelect.get(4),(Course)coursesToSelect.get(5)};
coursesToSelect.removeAll(Arrays.asList(courses));
}
public static void main(String[] args){
ListTest it = new ListTest();//创建ListTest的实例
it.TestAdd();
it.testGet();
it.testIterator();
it.testModify();
it.testRemove();
}
}
Student.java
import java.util.HashSet;
import java.util.Set;
public class Student {
/*
* 学生类
*/
private String id;
private String name;
public Set courses;
public Student(String id,String name){
this.id = id;
this.name = name;
this.courses = new HashSet();//Set是个接口,HashSet是Set的实现类
}
}