List接口的实现类

List接口的实现类

  • ArrayList【重点】:

    1. 数组结构实现,查询快,增删慢;

    2. 运行效率快,线程不安全

  • Vector:

    1. 数组结构实现,查询快,增删慢

    2. 运行效率慢,线程安全

  • LinkedList:

    1. 链表结构实现,增删快,查询慢。

ArrayList使用

示例代码;

/**
 * ArrayList的使用
 * 存储结构:数组
 */
public class HelloWorld {
    public static void main(String[] args) {
        //创建集合
        ArrayList arrayList = new ArrayList();
        //添加元素
        Student s1 = new Student("a",10);
        Student s2 = new Student("b",20);
        Student s3 = new Student("c",30);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        System.out.println(arrayList.size());
        System.out.println(arrayList.toString());
        System.out.println("-------------------------------");
        //删除元素
        //arrayList.remove(s1);
        arrayList.remove(new Student("a",10));  //重写equals方法
        System.out.println(arrayList.size());
        System.out.println("-------------------------------");
        //遍历【重点】
        //迭代器
        Iterator iterator = arrayList.iterator();
        while (iterator.hasNext()){
            Student s = (Student) iterator.next();
            System.out.println(s.toString());
        }
        System.out.println("-------------------------------");
        //列表迭代器  顺序  逆序
        ListIterator listIterator = arrayList.listIterator();
        while (listIterator.hasNext()){
            Student student = (Student) listIterator.next();
            System.out.println(student.toString());
        }

        while (listIterator.hasPrevious()){
            Student student = (Student) listIterator.previous();
            System.out.println(student.toString());
        }
        System.out.println("-------------------------------");
        //判断
        System.out.println(arrayList.contains("a"));
        System.out.println(arrayList.isEmpty());
        System.out.println(arrayList.contains(new Student("b",20)));
        System.out.println("-------------------------------");

        //查找
        System.out.println(arrayList.indexOf(s2));
        System.out.println(arrayList.indexOf(new Student("c",30)));
    }
}

equals方法重写代码:

 @Override
    public boolean equals(Object o) {
        if (this == o){
            return true;
        }
        if (o == null){
            return false;
        }
        if (o instanceof Student){
            Student s  = (Student) o;
            //比较属性
            if (this.name.equals(s.getName()) && this.age == s.getAge()){
                return true;
            }
        }
        //不满足返回false
        return false;
    }

LinkedList使用

示例代码:

/**
 * LinkedList的使用
 * 存储结构:双向链表
 */
public class HelloWorld {
    public static void main(String[] args) {
        //创建集合
        LinkedList linkedList = new LinkedList();
        //1.添加元素
        Student s1 = new Student("a",10);
        Student s2 = new Student("b",20);
        Student s3 = new Student("c",30);
        linkedList.add(s1);
        linkedList.add(s2);
        linkedList.add(s3);
        //linkedList.add(s3);//可重复
        System.out.println(linkedList.size());
        System.out.println(linkedList.toString());
        System.out.println("------------------------");
        //2.删除
        //linkedList.remove(s1);
        System.out.println(linkedList.size());
        //linkedList.clear();

        //3.遍历
        //3.1 for遍历
        for (int i = 0; i <linkedList.size() ; i++) {
            System.out.println(linkedList.get(i));
        }
        System.out.println("------------------------");
        //3.2增强for遍历
        for (Object o:linkedList) {
            Student s = (Student) o;
            System.out.println(s.toString());
        }
        System.out.println("------------------------");

        //3.3迭代器
        Iterator iterator = linkedList.iterator();
        while (iterator.hasNext()){
            System.out.println((Student)iterator.next());
        }
        System.out.println("------------------------");

        //ListIterator  列表迭代器
        ListIterator listIterator = linkedList.listIterator();
        while (listIterator.hasNext()){
            System.out.println((Student)listIterator.next());
        }
        System.out.println("------------------------");

        //判断
        System.out.println(linkedList.contains(s1));
        System.out.println(linkedList.isEmpty());
        //获取
        System.out.println(linkedList.indexOf(s2));
    }
}
原文地址:https://www.cnblogs.com/qiudajiang/p/13260998.html