Java(十二)--List的添加,修改,删除

首先我们先创建课程类

/* * 课程类 */
public class Course {
    public String id;
    public String name;

    Course(){
    }

    Course(String newID,String newName){
        id=newID;
        name=newName;
    }
}

接着创建ListTest类(含main方法)用来测试。
List相当于C++中的容器List,ListTest(以下简称LT)类中的属性与构造函数

/* *ArrayList是 动态数组,List的子接口,需要引入java.util.ArrayList */

public List coursesToSelect;
ListTest(){
        coursesToSelect=new ArrayList();
    }

ArrayList的介绍
http://baike.baidu.com/link?url=f-S9V_nz6q2DN0kQ42wCaMgrwgtuFtEn8icDKXa4Y4ZgdQkvzJ0euI0kN_Kq3Yj0On684jNBTDSZrDLk_PyaWK

ArrayList的一个方法是add(Object obj),将对象添加都尾部
add的另一个重载的方法是add(int index,Object obj),将对象添加到index位置,如果超出了数组的范围,会抛出异常。如果index位置已经有了,那么按顺序将原有的依次挤下去。代码如下

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);//数据结构移到1位置
        Course temp1=(Course)coursesToSelect.get(0);
        System.out.println("添加了课程:"+temp1.id+":"+temp1.name);
}

代码中用到了ArrayList中的另一个方法get(int index),该方法可以取出index位置的元素,但是返回的是Object类型的,所以需要强制类型转换

ArrayList还支持整个数组插进去。通过方法boolean addAll(Collection

Course[] course={new Course("3","离散数学"),new Course("4","汇编语言")};
        coursesToSelect.addAll(Arrays.asList(course));
        Course temp2=(Course)coursesToSelect.get(3);
        Course temp3=(Course)coursesToSelect.get(4);
        System.out.println("添加了课程:"+temp2.id+":"+temp2.name
                +":"+temp3.id+":"+temp3.name);

        Course[] course2={new Course("5","高等数学"),new Course("6","大学英语")};
        coursesToSelect.addAll(2,Arrays.asList(course2));
        Course temp4=(Course)coursesToSelect.get(2);
        Course temp5=(Course)coursesToSelect.get(3);
        System.out.println("添加了课程:"+temp4.id+":"+temp4.name
                +":"+temp5.id+":"+temp5.name);

当传数组给addAll时要先用Arrays中的asList方法,将数组转换成List。

List的修改
通过方法E set(int index, E element)来修改,添加方法

    /* * 修改list */
    public void testModify(){
        coursesToSelect.set(4, new Course("7","毛概"));

}

删除
使用remove和removeAll方法

/* * 删除list */
    public void testRemove(){
        //Course cr=(Course)coursesToSelect.get(4);
        //System.out.println("我是课程"+cr.name+"我即将被删除");
        //coursesToSelect.remove(4);
        Course[] course={(Course)coursesToSelect.get(4),(Course)coursesToSelect.get(5),(Course)coursesToSelect.get(6)};
        coursesToSelect.removeAll(Arrays.asList(course));
        System.out.println("成功删除");
    }

如果用往add方法中传进奇怪的东西比如字符串则会抛出异常,但程序仍然可以运行,这时我们要使用泛型编程,使用带泛型会在编译期间就进行类型检查,如果类型不对则无法通过编译,报错。新建一个类testGeneric,但要注意泛型不能使用基本类型,可以添加泛型子类型的对象的实例。比如我们新建Course的子类ChildCourse,无需任何东西。

import java.util.*;

public class testGeneric {

    /* * 带有泛型的courses */
    public Listcourses;

    public testGeneric(){
        courses=new ArrayList();
    }

    /* * 测试添加 */
    public void testAdd(){
        Course cr1=new Course("1","大学语文");
        courses.add(cr1);
        //带泛型会在编译期间进行类型检查
        //courses.add("afnaf");
        Course cr2=new Course("2","Java基础");
        courses.add(cr2);
    }

    public void testForEach(){
        for(Course obj: courses){//不需要类型转换,因为泛型是Course
            Course cr=obj;
            System.out.println("课程"+cr.id+":"+cr.name);
        }
    }

    /* * 泛型结合可以添加泛型的子类型的对象实例 */
    public void testChild(){
        ChildCourse ccr=new ChildCourse();
        ccr.id="3";
        ccr.name="我是子类型的实例";
        courses.add(ccr);
    }

    /* * 泛型不能使用基本类型 */
    public void testPrimitive(){
        List list=new ArrayList();
        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.testPrimitive();
        tg.testAdd();
        tg.testChild();
        tg.testForEach();
    }
}

你可能感兴趣的:(Java)