数据结构

  LinkedList是基于链表结构实现,所以在LinkedList类中包含了first和last两个指针(类型为Node)。Node中包含了对prev节点、next节点的引用,这样就构成了双向的链表。

   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;
}
}

存储

1.add(E e)方法

  该方法首先声明一个新Node l,将链表的最后一个Node赋值给l,然后将新的Node即newNode覆盖last(或者说让last指向newNode),最后将l的next指针指向newNode。

   //Appends the specified element to the end of this list.
public boolean add(E e) {
linkLast(e);
return true;
}
/**
* Links e as last element.
*/
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++;
}

2.add(int index, E element)

  该方法是在指定的index位置插入新元素。如果index位置正好等于size,则调用linkLast(element)将其插入到末尾;否则调用linkBefore(element, node(index))方法进行插入。

  linkBefore中的node(index)是返回指定位置的元素。

   public void add(int index, E element) {
checkPositionIndex(index);
if (index == size)
linkLast(element);
else
linkBefore(element, node(index));
}
/**
* Returns the (non-null) Node at the specified element index.
*/
Node<E> node(int index) {
// assert isElementIndex(index);
// 如果要取元素的位置是整个list一半的左半边,那么从list的头开始向后遍历,遍历至要取元素的位置
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
}
// 否则从list一半的右半边开始寻找,也就是从尾部开始向前遍历,遍历至要取元素的位置
else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}

删除

1.remove(int index)

  删除列表中指定位置的元素,首先检测该位置是否在该列表中存在,然后解除该元素前、后指向的元素。

   public E remove(int index) {
checkElementIndex(index);
return unlink(node(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;
}

获取

1.get(int index)

  获取指定位置元素的值。同样首先判断传入位置是否越界,如果超过list的size,抛出IndexOutBoundsException异常,然后node()方法进行左半边或右半边遍历获取,add(int index, E element)中有提到,不再赘述。

     public E get(int index) {
checkElementIndex(index);
return node(index).item;
}

2.getFirst()

3.getLast()

  first、last是LinkedList的两个属性,判空后直接返回。

     /**
* Returns the first element in this list.
*
* @return the first element in this list
* @throws NoSuchElementException if this list is empty
*/
public E getFirst() {
final Node<E> f = first;
if (f == null)
throw new NoSuchElementException();
return f.item;
} /**
* Returns the last element in this list.
*
* @return the last element in this list
* @throws NoSuchElementException if this list is empty
*/
public E getLast() {
final Node<E> l = last;
if (l == null)
throw new NoSuchElementException();
return l.item;
}

最新文章

  1. RandHelper
  2. [深入浅出WP8.1(Runtime)]Toast通知
  3. HttpURLConnection请求网络数据的Post请求
  4. Unity 3D 粒子系统的一点经验
  5. Oracle 连接 Visual Studio 的工具
  6. node-mysql中的连接池代码学习
  7. 两个数据库表同步的可视化WEB同步程序
  8. [置顶] Objective-C开发环境介绍以及Cocoa,以及第一个程序
  9. Unattended Setup Software Components (无人值守安装软件组件)
  10. 初学Python(十)——列表生成式
  11. 网络唤醒全攻略(Wake On Lan)
  12. Detect Capital
  13. java虚拟机参数设置 jvm参数设置
  14. c/c++语言开发工具Dev-cpp【推荐】
  15. 大学jsp实验4include,forword
  16. HDU 1301-Jungle Roads【Kruscal】模板题
  17. zabbix宏(macro)使用:自定义监控阈值
  18. Linux 时间矫正命令
  19. Android开发之SurfaceView
  20. tp5总结(一)

热门文章

  1. android图片等比例缩放 填充屏幕
  2. Linux批量kill进程
  3. java与C++之间进行SOCKET通讯要点简要解析
  4. Mapped Statements collection does not contain value for com.xxxx.dao.impl.AreaDAOImpl.findByCode
  5. T4文本模板转换过程
  6. Android新特性--ConstraintLayout完全解析
  7. nginx 读取文件 permission denied
  8. 用原生JavaScript写AJAX
  9. IOS 项目的瘦身工具
  10. macbook中vagrant升级新版本