温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

LinkedList如何在Java项目中使用

发布时间:2020-11-12 15:36:34 来源:亿速云 阅读:204 作者:Leah 栏目:编程语言

LinkedList如何在Java项目中使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

一、LinkedList概述:

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

二、LinkedList的实现:

1、构造方法

//构造一个空的LinkedList
public LinkedList() {
}
//接收一个Collection参数c,默认构造方法构造一个空的链表,并通过addAll将c中的元素全部添加到链表中。
public LinkedList(Collection<&#63; 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<&#63; extends E> c) {
  return addAll(size, c);
 }
//index参数指定collection中插入的第一个元素的位置
 public boolean addAll(int index, Collection<&#63; 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 是非同步的。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI