当前位置: 移动技术网 > IT编程>开发语言>Java > java LinkedList源码详解及实例

java LinkedList源码详解及实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

一、linkedlist概述:

linkedlist与arraylist一样,是实现了list接口。由于linkedlist是基于链表实现的,所以它执行插入和删除操作时比arraylist更高效,而随机访问的性能要比arraylist低。

二、linkedlist的实现:

1、构造方法

//构造一个空的linkedlist
public linkedlist() {
}
//接收一个collection参数c,默认构造方法构造一个空的链表,并通过addall将c中的元素全部添加到链表中。
public linkedlist(collection<? extends e> c) {
 this();
 addall(c);
}

2、一些方法

2.1 getfirst()

//获取linkedlist第一个元素,如果linkedlist为空,则抛出异常。
 public e getfirst() {
  final node<e> f = first;
  if (f == null)
   throw new nosuchelementexception();
  return f.item;
 }

2.2 getlast()

//获取getlast()最后一个元素,如果linkedlist为空,则抛出异常。
 public e getlast() {
  final node<e> l = last;
  if (l == null)
   throw new nosuchelementexception();
  return l.item;
 }

2.3 removefirst()

//删除linkedlist第一个元素,如果linkedlist为空,则抛出异常。
 public e removefirst() {
  final node<e> f = first;
  if (f == null)
   throw new nosuchelementexception();
  return unlinkfirst(f);
 }

2.4 removelast()

//删除linkedlist最后一个元素,如果linkedlist为空,则抛出异常。
 public e removelast() {
  final node<e> l = last;
  if (l == null)
   throw new nosuchelementexception();
  return unlinklast(l);
 }

2.5 addfirst(e e)

//在linkedlist开始的位置插入一个新元素。
 public void addfirst(e e) {
  linkfirst(e);
 }

2.6 addlast(e e)

//在linkedlist末尾的位置插入一个新的元素。
 public void addlast(e e) {
  linklast(e);
 }

2.7 contains(object o)

//判断linkedlist中是否包含某个元素,若包含返回true,否则返回false
 public boolean contains(object o) {
  return indexof(o) != -1;
 }

2.8 add(e e)

//在linkedlist末尾处添加一个新的元素。
 public boolean add(e e) {
  linklast(e);
  return true;
 }

2.9 remove(object o)

//删除linkedlist中第一个出现的指定元素,如果linkedlist中不包含该元素,则链表保持不变。
public boolean remove(object o) {
  if (o == null) {
   for (node<e> x = first; x != null; x = x.next) {
    if (x.item == null) {
     unlink(x);
     return true;
    }
   }
  } else {
   for (node<e> x = first; x != null; x = x.next) {
    if (o.equals(x.item)) {
     unlink(x);
     return true;
    }
   }
  }
  return false;
 }

2.10 addall()

//增加collection中的所有元素,
 public boolean addall(collection<? extends e> c) {
  return addall(size, c);
 }
//index参数指定collection中插入的第一个元素的位置
 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;
  if (index == size) {
   succ = null;
   pred = last;
  } else {
   succ = node(index);
   pred = succ.prev;
  }

  for (object o : a) {
   @suppresswarnings("unchecked") e e = (e) o;
   node<e> newnode = new node<>(pred, e, null);
   if (pred == 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.11 clear()

//删除linkedlist中的所有元素。
 public void clear() {
  //删除linkedlist中所有链接是没有必要的,但是:
  // 可以帮助分代gc,如果删除节点超过一代就会释放内存,因为有一个可用的迭代器。
  for (node<e> x = first; x != null; ) {
   node<e> next = x.next;
   x.item = null;
   x.next = null;
   x.prev = null;
   x = next;
  }
  first = last = null;
  size = 0;
  modcount++;
 }

2.12 get(int index)

//获取指定位置的节点
 public e get(int index) {
  checkelementindex(index);
  return node(index).item;
 }

2.13 set(int index, e element)

//给指定节点赋一个指定的值,替换原来的值。
 public e set(int index, e element) {
  checkelementindex(index);
  node<e> x = node(index);
  e oldval = x.item;
  x.item = element;
  return oldval;
 }

2.14 add(int index, e element)

//在指定位置插入一个指定的值,原来该位置上以后的节点依次向后移动一位。
 public void add(int index, e element) {
  checkpositionindex(index);

  if (index == size)
   linklast(element);
  else
   linkbefore(element, node(index));
 }

2.15 remove(int index)

//删除指定位置的值,该位置之后的节点依次向前移动一位。
 public e remove(int index) {
  checkelementindex(index);
  return unlink(node(index));
 }

三、总结

linkedlist,通俗的理解就是数据结构中讲的链表在java语言中的实现,所以在链表中所有操作,linkedlist中都有,其实现原理也都是基于数据结构中所讲的对链表中的节点插入、删除、移动等方法。
linkedlist 是一个继承于abstractsequentiallist的双向链表。它也可以被当作堆栈、队列或双端队列进行操作。
linkedlist 实现 list 接口,能对它进行队列操作。
linkedlist 实现 deque 接口,即能将linkedlist当作双端队列使用。
linkedlist 实现了cloneable接口,即覆盖了函数clone(),能克隆。
linkedlist 实现java.io.serializable接口,这意味着linkedlist支持序列化,能通过序列化去传输。
linkedlist 是非同步的。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网