当前位置:网站首页>Collection 接口 & List 接口

Collection 接口 & List 接口

2022-08-09 07:56:00 洛央虲

package com.imooc.collection;
/** * 课程类 * @author Monica * */
public class Course {
    
    public String id;
    public String name;
    public Course(String id,String name){
        this.id = id;
        this.name = name;
    }
    public Course(){

    }
}
package com.imooc.collection;

import java.util.HashSet;
import java.util.Set;
/** * 学生类 * @author Monica * */

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>();//set接口调用
    }
}

package com.imooc.collection;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ListTest {
    
    /** * 用于存放备选课程的List */
    public List coursesToSelect;

    public ListTest(){
        this.coursesToSelect = new ArrayList();
    }
    /** * 用于往coursesToSelect中添加备选课程 */
    public void testAdd(){
        //创建一个课程对象,并通过调用add方法,添加到备选课程List中
        Course cr1 = new Course("1","数据结构");
        coursesToSelect.add(cr1);
        //取出元素 get方法
        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);
        //取出元素 get方法
        Course temp0 = (Course)coursesToSelect.get(2);
        System.out.println("添加了课程:"+temp.id+":"+temp.name);

        //以下方法会抛出数组下标越界异常
        //Course cr3 = new Course("3","Test");
        //coursesToSelect.add(4,cr3);数组越界

        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);


    }
    /** * 取得List中的元素的方法 * @param args */
    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方法 * @param args */
    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方法访问集合元素 * @param args */
    public void testForEach(){
        System.out.println("有如下课程待选(通过for each访问):");
        for(Object obj:coursesToSelect){
            Course cr = (Course)obj;
            System.out.println("课程:"+cr.id+":"+cr.name);
        }
    }
    /** * 修改List中的元素 * @param args */
    public void testModify(){
        coursesToSelect.set(4, new Course("7","毛概"));
    }
    /** * 删除List中的元素 * @param args */
    public void testRemove(){
        Course cr = (Course)coursesToSelect.get(4);
        System.out.println("我是课程:"+cr.id+":"+cr.name+"我即将被删除");
        coursesToSelect.remove(cr);
        System.out.println("成功删除课程!");
        testForEach();
    }
    public void testRemoves(){
        System.out.println("即将删除4位置上的元素");
        coursesToSelect.remove(4);
        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();
    }
}
package com.imooc.collection;

public class ChildCourse extends Course {
    

}
package com.imooc.collection;

import java.util.ArrayList;
import java.util.List;

public class TestGeneric {
    
    /** * 带有泛型——Course,的List类型属性 */
    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 testForEach(){
        for(Course cr:courses){
            System.out.println(cr.id+":"+cr.name);
        }
    }

    /** * 泛型集合可以添加泛型的子类型的对象实例 * @param args */
    public void testChild(){
        ChildCourse ccr = new ChildCourse();
        ccr.id = "3";
        ccr.name = "我是子类型的课程对象实例";
        courses.add(ccr);
    }
    /** * 泛型不能使用基本类型 * @param args */
    public void testBasicType(){
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        System.out.println("基本类型必须使用包装类作为泛型!"+list.get(0));
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TestGeneric tg = new TestGeneric();
        tg.testAdd();
        tg.testForEach();
        tg.testChild();
        tg.testForEach();
        tg.testBasicType();

    }

}
原网站

版权声明
本文为[洛央虲]所创,转载请带上原文链接,感谢
https://blog.csdn.net/mengwuyoulin/article/details/51599201