Java List 增删改查

定义2个类,课程类和选课类

package com.imooc.collection;

/**
 * 课程类
 */

public class Course {
    private String id;
    private String name;

    public Course(){

    }

    public Course(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public void setId(String id){
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}
package com.imooc.collection;

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

/**
 * 备选课程类
 * List是一个接口类,不能直接实例化,需要使用它的实现类 ArrayList或LinkedList来进行实例化
 */
public class ListTest {
    /**
     * 用于存放备选课程的List
     */
    private List coursesToSelect;

    public ListTest(){
        this.coursesToSelect = new ArrayList();
    }

    public List getCoursesToSelect() {
        return coursesToSelect;
    }

    // 用于往courseToSelect中添加备选课程
    public void testAdd(){
        // 创建一个课程对象,并通过调用add方法,添加到备选课程List中
        Course cr1 = new Course("1", "数据结构");
        coursesToSelect.add(cr1);
        Course temp = (Course) coursesToSelect.get(0);
        System.out.println("添加了课程:" + temp.getId() + ":" + temp.getName());

        Course cr2 = new Course("2", "C语言");
        coursesToSelect.add(0, cr2);
        Course temp2 = (Course) coursesToSelect.get(0);
        System.out.println("添加了课程:" + temp2.getId() + ":" + temp2.getName());

        // Course数组
        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.getName() + ":" + temp4.getName());

        Course[] course2 = {new Course("5", "高等数学"), new Course("6", "大学英语")};
        coursesToSelect.addAll(2, Arrays.asList(course2));

    }

    /**
     * 获取List中元素的方法
     */
    public void testGet(){
        int size = coursesToSelect.size();
        for(var i=0; i<size; i++){
            Course cr = (Course) coursesToSelect.get(i);
            System.out.println("课程:" + cr.getId() + ":" + cr.getName());
        }
    }

    /**
     * 通过迭代器来遍历List
     */
    public void testIterator(){
        Iterator it = coursesToSelect.iterator();
        System.out.println("(iterator)有如下课程待选:");
        while (it.hasNext()){
            Course cr = (Course) it.next();
            System.out.println("课程:" + cr.getId() + ":" + cr.getName());

        }
    }

    /**
     * 通过 foreach 方法来遍历List
     * @param args
     */
    public void testForeach(){
        System.out.println("(foreach)有如下课程待选:");
        for (Object obj: coursesToSelect) {
            Course cr = (Course) obj;
            System.out.println("课程:" + cr.getId() + ":" + cr.getName());

        }
    }

    /**
     * 修改 List 中的元素
     * @param args
     */
    public void testModify(){
        coursesToSelect.set(0, new Course("2", "Python之美"));
    }

    /**
     * 删除 List 中的元素
     * @param args
     */
    public void testDelete(){
        Course cr = (Course) coursesToSelect.get(0);
        coursesToSelect.remove(cr);
        coursesToSelect.remove(0);

        Course[] courses = {(Course) coursesToSelect.get(0), (Course) coursesToSelect.get(1)};
        coursesToSelect.removeAll(Arrays.asList(courses));
    }

    public static void main(String args[]){
        ListTest lt = new ListTest();
        lt.testAdd();
        lt.testGet();
        lt.testIterator();
        lt.testForeach();

        lt.testModify();

        lt.testDelete();
        lt.testForeach();

    }
}

 

posted @ 2018-12-19 21:55  Vincen_shen  阅读(4854)  评论(0编辑  收藏  举报