Java集合——List实现类

参考视频:List 实现类

一、List常用实现类

1、ArrayList【重点】

  • 数组结构实现,查询快,增删慢;
  • jdk1.2版本,运行效率快,线程不安全。

2、Vector

  • 数组结构实现,查询快,增删慢;
  • jdk1.0版本,运行效率慢,线程不安全。(现在已经很少使用了)

3、LinkedList

  • 双向链表结构实现,无需连续空间,增删快,查询慢。

二、List实现类的使用

1、ArrayList

首先创建一个student类:

package List;

import java.util.Objects;

public class Student {
    
    
    private String name;
    private Integer age;

    public Integer getAge() {
    
    
        return age;
    }

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


    public String getName() {
    
    
        return name;
    }

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

	//alt + insert 创建有参构造方法
    public Student(String name, Integer age) {
    
    
        this.name = name;
        this.age = age;
    }


	// alt + insert 重写toString() 方法
    @Override
    public String toString() {
    
    
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    // 输入equals,按enter回车
    // 或者 alt + insert 重写equals() 方法
    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Student student = (Student) o;
        return Objects.equals(name, student.name) && Objects.equals(age, student.age);
    }


}

ArrayList 使用:

package List;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

/**
 * ArrayList 使用
 * 存储结构:数组
 * 特点:查找遍历速度快,增删速度慢
 */


public class List {
    
    

    public static void main(String[] args) {
    
    
        //创建集合
        ArrayList arrayList =new ArrayList<>();

        //1、添加元素
        Student s1 = new Student("刘德华",33);
        Student s2 = new Student("郭富城",2);
        Student s3 = new Student("张三",24);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        System.out.println("元素个数:"+arrayList.size()); //元素个数:3
        System.out.println("元素:"+arrayList.toString());
        //元素:[Student{name='刘德华', age=33}, Student{name='郭富城', age=2}, Student{name='张三', age=24}]


        //2、删除
        // 使用这种删除方式,需要在student类里重写equals方法
        arrayList.remove(new Student("刘德华",33));
        System.out.println("删除之后:"+arrayList.toString());
        //删除之后:[Student{name='郭富城', age=2}, Student{name='张三', age=24}]


        //3、遍历
        //3.1 迭代器
        Iterator it = arrayList.iterator();
        while (it.hasNext()){
    
    
            String s = it.next().toString();
            System.out.println(s);
        }
        //Student{name='郭富城', age=2}
        //Student{name='张三', age=24}

        //3.2 列表迭代器(可以逆序)
        System.out.println("==========列表迭代=========");
        ListIterator listIterator = arrayList.listIterator();
        while (listIterator.hasNext()){
    
    
            String s = listIterator.next().toString();
            System.out.println(s);
        }
		// Student{name='郭富城', age=2}
		// Student{name='张三', age=24}
		
        System.out.println("==========列表迭代逆序=========");
        while (listIterator.hasPrevious()){
    
    
            String s = listIterator.previous().toString();
            System.out.println(s);
        }
        //Student{name='张三', age=24}
        //Student{name='郭富城', age=2}


        //4、判断
        System.out.println(arrayList.contains(s2)); //true
        System.out.println(arrayList.contains(new Student("郭富城",2))); //true,这是因为在student里面重写了equals方法

        //5、查找
        System.out.println(arrayList.indexOf(new Student("郭富城",2))); // 0
        System.out.println(arrayList.indexOf(new Student("张三",24)));  // 1

    }
}

3、LinkedList

package List;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

/**
 * LinkedList
 * 存储结构:双向链表
 * 特点:查询慢,增删快
 */

public class LinkedListdemo {
    
    

    public static void main(String[] args) {
    
    
        //创建
        LinkedList linkedList = new LinkedList<>();


        //1、添加
        Student s1 = new Student("刘德华",33);
        Student s2 = new Student("郭富城",2);
        Student s3 = new Student("张三",24);
        linkedList.add(s1);
        linkedList.add(s2);
        linkedList.add(s3);
        System.out.println(linkedList.toString());
        //[Student{name='刘德华', age=33}, Student{name='郭富城', age=2}, Student{name='张三', age=24}]

        //2、删除
        //linkedList.remove(s1);
        linkedList.remove(new Student("刘德华",33));
        System.out.println("删除之后:"+linkedList.size()); // 删除之后:2
        System.out.println(linkedList.toString()); // [Student{name='郭富城', age=2}, Student{name='张三', age=24}]

        //3、遍历
        System.out.println("========= 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 object:linkedList) {
    
    
            String s = object.toString();
            System.out.println(s);
        }

        System.out.println("========= 3.3 迭代器 ========");
        Iterator iterator = linkedList.iterator();
        while (iterator.hasNext()){
    
    
            String s = iterator.next().toString();
            System.out.println(s);
        }

        System.out.println("========= 3.4 ListIterator 迭代器 ========");
        // 可以正序,也可以逆序
        // 逆序的话,需要将listIterator的index值设置为linkedList.size()
        // 因为默认index是0,此时指针在第一个位置,listIterator.hasPrevious()是false
        ListIterator listIterator = linkedList.listIterator(linkedList.size());
        while (listIterator.hasPrevious()){
    
    
            String s = listIterator.previous().toString();
            System.out.println(s);
        }
        //Student{name='张三', age=24}
        //Student{name='郭富城', age=2}

        //4、判断
        System.out.println(linkedList.contains(s2)); //true
        System.out.println(linkedList.isEmpty()); //false

        //5、获取
        System.out.println(linkedList.indexOf(s2));//0


    }
}

三、ArrayList 和 LinkedList 区别

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/lixingecho/article/details/119816554