workplace-blog

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

LinkedList

1.特点:

  • 链表结构实现,查询慢,增删快

2.LinkedList使用

/*
LinkedList的使用
 */
public class linkedListDemo01 {
    public static void main(String[] args) {
        //创建ArrayList对象
        LinkedList list = new LinkedList();
        student s1 = new student("张三", 19);
        student s2 = new student("李四", 19);
        student s3 = new student("王五", 19);
        student s4 = new student("赵六", 22);
        //1.添加
        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        System.out.println(list);

        /*
        2.删除
        remove方法底层调用了Object的equals方法,比较的是对象的引用
        arrayList.remove(new student("张三",19));无法删除new出来的新对象
        若想删除需要重写equals方法
         */
        list.remove(s1);
        list.remove(new student("张三", 19));//成功删除
        System.out.println("-------2.删除---------");
        System.out.println(list);

        //3.遍历
        //3.1使用迭代器
        Iterator it = list.iterator();
        System.out.println("-------3.1使用迭代器---------");
        while (it.hasNext()) {
            student s = (student) it.next();
            System.out.println(s);
        }
        //3.2使用列表迭代器
        ListIterator lit = list.listIterator();
        System.out.println("-------3.2使用列表迭代器--------");
        while (lit.hasNext()) {
            student s = (student) lit.next();
            System.out.println(s);
        }
        //3.3for循环遍历
        System.out.println("-------3.3for循环遍历--------");
        for (int i = 0; i <list.size() ; i++) {
            System.out.println(list.get(i));
        }

        //4.判断
        System.out.println("---------4.判断--------");
        System.out.println(list.contains(new student("赵六", 22)));

        //5.查找
        System.out.println("---------5.查找--------");
        System.out.println(list.indexOf(s2));
    }
}

//输出结果:
[student{name='张三', age=19}, student{name='李四', age=19}, student{name='王五', age=19}, student{name='赵六', age=22}]
-------2.删除---------
[student{name='李四', age=19}, student{name='王五', age=19}, student{name='赵六', age=22}]
-------3.1使用迭代器---------
student{name='李四', age=19}
student{name='王五', age=19}
student{name='赵六', age=22}
-------3.2使用列表迭代器--------
student{name='李四', age=19}
student{name='王五', age=19}
student{name='赵六', age=22}
-------3.3for循环遍历--------
student{name='李四', age=19}
student{name='王五', age=19}
student{name='赵六', age=22}
---------4.判断--------
true
---------5.查找--------
0

3.LinkedList源码分析:

  • add():(尾插法)
public boolean add(E e) {
    linkLast(e);
    return true;
}

void linkLast(E e) {
        final Node<E> l = last;
        final Node<E> newNode = new Node<>(l, e, null);
        last = newNode;
        if (l == null)
            first = newNode;
        else
            l.next = newNode;
        size++;
        modCount++;
}
    
//核心方法Node<E>
private static class Node<E> {
        E item;
        Node<E> next;
        Node<E> prev;

        Node(Node<E> prev, E element, Node<E> next) {
            this.item = element;
            this.next = next;
            this.prev = prev;
        }
    }

4.LinkedList和ArrayList的区别

posted on 2022-08-26 19:26  多巴胺LLL  阅读(30)  评论(0)    收藏  举报