LinkedList知识点

LinkedList类继承了AbstractSequentialList抽象类,同时继承了List,Deque,Cloneable,Serializable接口。

LinkedList采用的双向链表进行存储,结点用静态内部类Node。

源码如下:

private static class Node {
    E item;
    Nodenext; //后继节点
    Nodeprev; //前驱节点
    Node(Node prev, E element, Node next) {
        this.item = element;
        this.next = next;
        this.prev = prev;
    }
}

LinkedList有两个构造方法:

一 构造方法

1.

public LinkedList() {

}

无参构造方法

2.

public LinkedList(Collection c) {
   this();
    addAll(c);
}

继承Collection接口的子类均可以被转换成LinkedList

二 实例方法

public E getFirst() ;

得到头节点,此节点在LinkedList中第一次插入时就赋值给了first,直接传出即可

public E getLast() ;

得到尾节点,和getFirst() 相同,LinkedList中最后一个节点记为last

public E removeFirst() ; 

移除头节点,主要调用 private E unlinkFirst(Node f) ;方法,让移除的头节点的属性均等于null,目的是为了帮助GC

public void addFirst(E e) ;

插入作为第一个结点,调用的是linkFirst(e)方法

public void addLast(E e) ;

插入作为最后一个结点,调用的是linkLast(e)方法

public boolean contains(Object o) ;

查看LinkedList中是否含有此对象,调用的是indexOf(o) ; 方法判断返回对象的位置,如果不存在就返回-1

public int size() ;

返回LinkedList的长度

public boolean add(E e) ; 

调用linkLast(e)方法

public boolean remove(Object o) ; 

主要是调用 E unlink(Node x) 方法,双向链表删除一个结点

public boolean addAll(Collection c) ;

增添继承Collection接口子类的数据,将其转化为链表的结点,用public boolean addAll(int index, Collection c)方法添加

public boolean addAll(int index, Collection c) ; //在固定位置添加Collection接口子类的数据

public void clear();

清除LinkedList中所有元素,将所有结点中的数据都赋值为null,帮助GC进行清理

public E get(int index);

得到固定位置的元素,主要调用的是Node<E> node(int index);方法,如果位置在前一半从头循环,否则从尾循环

public int indexOf(Object o);

找到此对象的位置,如果不存在返回-1,否则返回对象的位置

public int lastIndexOf(Object o);

此对象最后一次出现的位置

public Object[] toArray();

将所有结点以数组的方式返回,返回的是Object数组

public <T> T[] toArray(T[] a);

将所有结点以数组的方式返回

队列的方法

public E peek();

返回第一个元素,如果LinkedList为空,返回null

public E element();

返回第一个元素,如果LinkedList为空,抛出异常

public E poll();

返回并删除第一个元素,如果LinkedList为空,返回null

public E remove() ;

返回并删除第一个元素,如果LinkedList为空,抛出异常

public boolean offer(E e);

将元素加入到LinkedList尾部,调用的public boolean add(E e);方法

public boolean offerFirst(E e);

将指定元素插入LinkedList的首部,调用的是public void addFirst(E e); 方法

public boolean offerLast(E e);

将指定元素插入LinkedList的尾部,调用的是public void addLast(E e);方法

public E peekFirst();

栈的方法

//压栈
public void push(E e);
//出栈
public E pop();

ListItr

ListItr继承了ListIterator接口
ListIterator继承了Iterator接口
LinkedList中获得ListItr实例的方法

public ListIterator<E> listIterator(int index) {
     checkPositionIndex(index);
     return new ListItr(index);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值