溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

LinkedList如何在Java項目中使用

發(fā)布時間:2020-11-12 15:36:34 來源:億速云 閱讀:213 作者:Leah 欄目:編程語言

LinkedList如何在Java項目中使用?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

一、LinkedList概述:

LinkedList與ArrayList一樣,是實現(xiàn)了List接口。由于LinkedList是基于鏈表實現(xiàn)的,所以它執(zhí)行插入和刪除操作時比ArrayList更高效,而隨機訪問的性能要比ArrayList低。

二、LinkedList的實現(xiàn):

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中第一個出現(xiàn)的指定元素,如果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,如果刪除節(jié)點超過一代就會釋放內存,因為有一個可用的迭代器。
  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)

//獲取指定位置的節(jié)點
 public E get(int index) {
  checkElementIndex(index);
  return node(index).item;
 }

2.13 set(int index, E element)

//給指定節(jié)點賦一個指定的值,替換原來的值。
 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)

//在指定位置插入一個指定的值,原來該位置上以后的節(jié)點依次向后移動一位。
 public void add(int index, E element) {
  checkPositionIndex(index);

  if (index == size)
   linkLast(element);
  else
   linkBefore(element, node(index));
 }

2.15 remove(int index)

//刪除指定位置的值,該位置之后的節(jié)點依次向前移動一位。
 public E remove(int index) {
  checkElementIndex(index);
  return unlink(node(index));
 }

三、總結

LinkedList,通俗的理解就是數據結構中講的鏈表在Java語言中的實現(xiàn),所以在鏈表中所有操作,LinkedList中都有,其實現(xiàn)原理也都是基于數據結構中所講的對鏈表中的節(jié)點插入、刪除、移動等方法。
LinkedList 是一個繼承于AbstractSequentialList的雙向鏈表。它也可以被當作堆棧、隊列或雙端隊列進行操作。
LinkedList 實現(xiàn) List 接口,能對它進行隊列操作。
LinkedList 實現(xiàn) Deque 接口,即能將LinkedList當作雙端隊列使用。
LinkedList 實現(xiàn)了Cloneable接口,即覆蓋了函數clone(),能克隆。
LinkedList 實現(xiàn)java.io.Serializable接口,這意味著LinkedList支持序列化,能通過序列化去傳輸。
LinkedList 是非同步的。

看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI