美文网首页
Java LinkedList集合

Java LinkedList集合

作者: 海人为记 | 来源:发表于2018-07-13 22:18 被阅读2次

LinkedList介绍

LinkedList是一个继承于AbstractSequentialList的双向链表。它也可以被当作堆栈、队列或双端对列进行操作。
LinkedLIst实现List接口,能对它进行队列操作。
LinkedList实现Deque接口。即能将LinkedList当作双端队列使用
LinkedList实现了Cloneable接口,即覆盖了函数clone(),能克隆
LinkedList实现java.io.Serializable接口,这意味着LinkedList支持序列化,能通过序列化去传输。
LinkedList是非同步的

这种数据结构有这样的特性:

  • 分配内存空间不是必须连续的
  • 插入、删除操作很快,只要修改前后指针就好,时间复杂度为O(1)
  • 访问比较慢,必须得从第一个元素开始遍历,时间复杂度为O(n)
    在Java中,LinkedList提供了丰富的方法,可以模拟链式队列,链式堆栈等数据结构,为用户带来了极大的方便。

LinkedList的方法

构造方法

//构造一个空列表。
public LinkedList() {
        header.next = header.previous = header;
}
    
//构造一个包含指定 collection 中的元素的列表,这些元素按其 collection 的迭代器返回的顺序排列。
public LinkedList(Collection<? extends E> c) {
        this();
        addAll(c);
}

代码示例:

        //空参构造方法
        LinkedList<String> c = new LinkedList<>();
        c.add("小红");
        c.add("小明");
        c.add("小光");
        c.add("小钱");
        //带参构造方法,构造一个包含指定 collection中的元素,这些元素按其collection的迭代器的返回的顺序排列
        LinkedList<String> linkedList = new LinkedList<>(c);
        System.out.println(linkedList);

LinkedList使用

import java.util.LinkedList;

public class LinkedListLean {

    public static void main(String[] args) {
        
        //空参构造方法
        LinkedList<String> c = new LinkedList<>();
        c.add("小红");
        c.add("小明");
        c.add("小光");
        c.add("小钱");
        //带参构造方法,构造一个包含指定 collection中的元素,这些元素按其collection的迭代器的返回的顺序排列
        LinkedList<String> linkedList = new LinkedList<>(c);
        System.out.println(linkedList);
        //获取但不溢出此列表的头
        String peek = linkedList.peek();
        System.out.println(peek);
        System.out.println(linkedList);
        //获取并移除此列表的头
        String poll = linkedList.poll();
        System.out.println(poll);
        System.out.println(linkedList);
        //获取并移除此列表的头
        String remove = linkedList.remove();
        System.out.println(remove);
        System.out.println(linkedList);
        //添加指定 collection 中的所有元素到此列表的结尾,顺序是指定collection迭代器返回的顺序
        boolean addAll =linkedList.addAll(c);
        System.out.println(addAll);
        //将指定 collection 中所有元素从指定位置开始插入此列表,其中index表示在其中插入指定collection中第一个元素的索引
        boolean addAlll = linkedList.addAll(1, c);
        System.out.println(addAlll);
        //将指定元素插入此列表的开头
        linkedList.addFirst("小赵");
        System.out.println(linkedList);
        //将指定元素添加到此列表的结尾
        linkedList.addLast("小孙");
        System.out.println(linkedList);
        //从此列表中移除所有元素
        linkedList.clear();
        //获取并移除此列表的头
        linkedList.remove();
        //移除此列表中指定位置 处的元素
        linkedList.remove(1);
        //从此列表中移除首次出现的指定元素
        linkedList.remove("小兰");
        //移除并返回此列表的第一个元素
        String first = linkedList.removeFirst();
        System.out.println(first);
        //移除并返回此列表的最后一个元素
        String last = linkedList.removeLast();
        System.out.println(last);
        //从列表中移除第一个指定的元素(从表头遍历到表尾)
        linkedList.removeFirstOccurrence("小明");
        //从列表中移除最后一次出现的指定元素(从表头遍历到表尾)
        linkedList.removeLastOccurrence("小光");
        
        //返回此列表中指定位置处的元素
        String get = linkedList.get(1);
        System.out.println(get);
        //返回此列表的第一个元素
        String firstGet = linkedList.getFirst();
        System.out.println(firstGet);
        //返回此列表的最后一个元素
        String lastGet = linkedList.getLast();
        System.out.println(lastGet);
        //返回此列表中首次出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1
        int index = linkedList.indexOf("小明");
        System.out.println(index);
        //返回此列表中最后出现的指定元素的索引,如果此列表中不包含该元素,则返回 -1
        int lastIndex = linkedList.lastIndexOf("小明");
        System.out.println(lastIndex);
    }
}

相关文章

网友评论

      本文标题:Java LinkedList集合

      本文链接:https://www.haomeiwen.com/subject/renspftx.html