← 返回首页
JavaSE系列教程(三十八)
发表时间:2020-01-17 23:37:06
讲解Java集合框架之List常用方法

介绍List接口常用方法。

List常用方法如下:

方法名字 说明
size() 获取长度
add(Object element) 添加元素
add(int index,Object element) 指定位置插入元素
subList(int start,int end) 获取子集合
remove(Object element) 删除元素(仅仅删除符合条件的第一个元素)
remove(int index) 删除指定索引的元素
removeAll(Collection list) 删除所有指定的元素
contains(Object obj) 判断指定元素是否存在
toArray() 序列转换为数组
isEmpty() 判断序列是否为空
clear() 清空序列

通过一个实例理解以上所有方法:


class Students{

    private String name;
    private String gender;
    private int age;

    public Students(String name, String gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Students{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}

public class Test2 {

    public static void main(String[] args) {

        List list = new LinkedList();
        Students s1 = new Students("张三","男",20);
        Students s2 = new Students("李四","女",18);
        Students s3 = new Students("王五","男",20);
        Students s4 = new Students("赵六","女",22);

        System.out.println("----------添加元素-----------");
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s1);
        list.add(s4);

        System.out.println("序列长度是:"+list.size());

        for(Object obj : list){
            System.out.println(obj);
        }
        System.out.println("----------指定位置插入新元素-----------");
        list.add(1,new Students("王大锤","男",19));

        for(Object obj : list){
            System.out.println(obj);
        }

        System.out.println("----------获取子集合--------");
        List subList =  list.subList(1,list.size());
        for(Object obj : subList){
            System.out.println(obj);
        }

        System.out.println("-------------删除元素------------");

        list.remove(s1); //删除张三对象,仅仅删除掉第一个张三对象
        //list.removeAll(Arrays.asList(new Students[]{s1})); //删除所有的张三对象

        for(Object obj : list){
            System.out.println(obj);
        }

        System.out.println(list.contains(s4));

        System.out.println(Arrays.toString(list.toArray()));

        System.out.println(list.isEmpty());

        list.clear();

        System.out.println(list.isEmpty());

    }
}

运行结果:
----------添加元素-----------
序列长度是:5
Students{name='张三', gender='男', age=20, birthday='null'}
Students{name='李四', gender='女', age=18, birthday='null'}
Students{name='王五', gender='男', age=20, birthday='null'}
Students{name='张三', gender='男', age=20, birthday='null'}
Students{name='赵六', gender='女', age=22, birthday='null'}
----------指定位置插入新元素-----------
Students{name='张三', gender='男', age=20, birthday='null'}
Students{name='王大锤', gender='男', age=19, birthday='null'}
Students{name='李四', gender='女', age=18, birthday='null'}
Students{name='王五', gender='男', age=20, birthday='null'}
Students{name='张三', gender='男', age=20, birthday='null'}
Students{name='赵六', gender='女', age=22, birthday='null'}
----------获取子集合--------
Students{name='王大锤', gender='男', age=19, birthday='null'}
Students{name='李四', gender='女', age=18, birthday='null'}
Students{name='王五', gender='男', age=20, birthday='null'}
Students{name='张三', gender='男', age=20, birthday='null'}
Students{name='赵六', gender='女', age=22, birthday='null'}
-------------删除元素------------
Students{name='王大锤', gender='男', age=19, birthday='null'}
Students{name='李四', gender='女', age=18, birthday='null'}
Students{name='王五', gender='男', age=20, birthday='null'}
Students{name='张三', gender='男', age=20, birthday='null'}
Students{name='赵六', gender='女', age=22, birthday='null'}
true
[Students{name='王大锤', gender='男', age=19, birthday='null'}, Students{name='李四', gender='女', age=18, birthday='null'}, Students{name='王五', gender='男', age=20, birthday='null'}, Students{name='张三', gender='男', age=20, birthday='null'}, Students{name='赵六', gender='女', age=22, birthday='null'}]
false
true