Collection学习(二)

一、概述

LinkedList源码分析:
public class LinkedList<E>extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable,java.io.Serializable
LinkedList是一个双向链表,继承自AbstractSequentialList<E>,并实现了多个接口

1、构造方法
public LinkedList() {
}
//将集合传入构造方法,转换成链表结构
public LinkedList(Collection<? extends E> c) {
this();
addAll(c);
}

public boolean addAll(Collection<? extends E> c) {
return addAll(size, c);
}
//将集合转换为链表
public boolean addAll(int index, Collection<? extends E> c) {
checkPositionIndex(index);

Object[] a = c.toArray();
int numNew = a.length;
if (numNew == 0)
    return false;

Node<E> pred, succ;//succ保存当前角标所对应的元素
if (index == size) {
    succ = null;
    pred = last;//当index == size 的时候,当前元素的上一个元素就是之前已存在链表的最后一个元素
} else {
 //node方法的主要作用就是判断index所处位置是在之前链表的上半部分还是下半部分,
//在上半部分就从第一个元素开始循环,循环到index位置时返回元素,
//如果是在后半部分,那么就从最后一个元素往前循环,循环到index位置时返回元素,这样可以提高效率
    succ = node(index);
    pred = succ.prev; //得到index所在元素的上一个元素引用
}

for (Object o : a) {
    @SuppressWarnings("unchecked") E e = (E) o;
    Node<E> newNode = new Node<>(pred, e, null);
    if (pred == null)  //如果前一个元素为null, 那么说明不存在链表,将该元素设置为链表的第一个元素
	first = newNode;
    else
	pred.next = newNode; //如果前一个元素存在,那么就从前一个元素的角标处插入
    pred = newNode;
}

if (succ == null) {
    last = pred;
} else {
    pred.next = succ;
    succ.prev = pred;
}

size += numNew;
modCount++;
return true;
}
2.存储方式
add方法:
 public void add(int index, E element) {
        checkPositionIndex(index);

        if (index == size) //当index == size的时候,已存在链表的最后一个元素就是当前待插入元素的上一个节点
            linkLast(element);
        else
            linkBefore(element, node(index));
    }

 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++;
    }
  void linkBefore(E e, Node<E> succ) {
        // assert succ != null;
        final Node<E> pred = succ.prev;
        final Node<E> newNode = new Node<>(pred, e, succ);
        succ.prev = newNode;
        if (pred == null)
            first = newNode;
        else
            pred.next = newNode;
        size++;
        modCount++;
    }
remove方法:
public E remove(int index) {
checkElementIndex(index);
return unlink(node(index));//node(index)获取index位置的节点
}
//
 E unlink(Node<E> x) {
        // assert x != null;
        final E element = x.item;
        final Node<E> next = x.next; //获取该节点的下一个元素
        final Node<E> prev = x.prev; //获取该节点的上一个元素

        if (prev == null) {
            first = next;
        } else {
            prev.next = next;
            x.prev = null;
        }

        if (next == null) {
            last = prev;
        } else {
            next.prev = prev;
            x.next = null;
        }

        x.item = null;
        size--;
        modCount++;
        return element;
    }

Set方法:
//替换该列表中的指定元素
public E set(int index, E element) {
checkElementIndex(index);
Node<E> x = node(index); 
E oldVal = x.item;
x.item = element;
return oldVal;
}
get方法:
public E get(int index) {
checkElementIndex(index);
return node(index).item;
}

二、LinkedList和ArrayList

1、ArrayList基于数组实现,因此具有: 有序、元素可重复、插入慢、 索引快 这些数组的特性; 
2、LinkedList 基于双向链表实现, 因此具有链表 插入快、 索引慢的特性;
3.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。 
4.对于新增和删除操作add和remove,LinedList比较占优势,因为ArrayList要移动数据。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值