Java-List测试-学生选课

package collection;

import java.util.*;
//备选课程类
public class ListTest {
    //用于存放备选课程的List
    public List coursesToSelect;
    public ListTest()
    {
        this.coursesToSelect = new ArrayList();
    }
    //用于添加备选课程
    public void testAdd()
    {
        //创建一个课程对象 并通过调用add方法添加到List中
        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);//插入到List的第一个位置
        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","test");
        //coursesToSelect.add(4,cr3);报错 List长度此时为2(元素占位为0,1) 》1或《0越界

        Course[] course = {new Course("3","离散数学"),new Course("4","汇编语言")};
        coursesToSelect.addAll(Arrays.asList(course));
        Course temp3 = (Course)coursesToSelect.get(3);
        Course temp4 = (Course)coursesToSelect.get(4);
        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);
    }

    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()
    {
        System.out.println("有如下课程待选:(通过迭代器)");
        Iterator it = coursesToSelect.iterator();
        while(it.hasNext())
        {
            Course cr = (Course) it.next();
            System.out.println("课程:"+cr.id+":"+cr.name);
        }
    }

    //通过for each方法访问集合元素
    public void testForEach()
    {
        System.out.println("有如下课程待选:(通过for each)");
        for(Object obj:coursesToSelect)//存入集合被当做Object
        {
            Course cr = (Course)obj;
            System.out.println("课程:"+cr.id+":"+cr.name);
        }
    }

    //修改集合元素
    public void testModify()
    {
        coursesToSelect.set(4,new Course("7","毛概"));
    }

    //删除List中元素
    public void testRemove()
    {
        //Course cr = (Course) coursesToSelect.get(4);
        //coursesToSelect.remove(cr);
        //以上等价于→
        //coursesToSelect.remove(4);

        Course[] courses={(Course) coursesToSelect.get(4),(Course) coursesToSelect.get(5)};
        coursesToSelect.removeAll(Arrays.asList(courses));//将courses数组转化为集合
        System.out.println("成功删除课程");
        System.out.println("剩余课程:");
        testForEach();
    }

    //往List添加非课程对象【?
    public void testType()
    {
        coursesToSelect.add("0-0?");
    }//显然是不能强转为course类型的 报错 使用泛型避免

    public static void main(String[] args)
    {
        ListTest lt = new ListTest();
        lt.testAdd();
        //lt.testType();
        //lt.testForEach();
        lt.testGet();
        lt.testIterator();
        lt.testModify();
        lt.testForEach();
        lt.testRemove();
    }
}
package collection;
//课程类
public class Course {
    public String id;
    public String name;
    public Course(String id,String name)
    {
        this.id = id;
        this.name = name;
    }

    public Course()
    {

    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Course))
            return false;
        Course other = (Course) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
}

你可能感兴趣的:(ArrayList)