最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java  LinkedList源碼詳解及實例

 更新時間:2017年03月18日 10:37:53   作者:wangshuang1631  
這篇文章主要介紹了java LinkedList源碼詳解及實例的相關(guān)資料,需要的朋友可以參考下

一、LinkedList概述:

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

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

1、構(gòu)造方法

//構(gòu)造一個空的LinkedList
public LinkedList() {
}
//接收一個Collection參數(shù)c,默認構(gòu)造方法構(gòu)造一個空的鏈表,并通過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中第一個出現(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<? extends E> c) {
  return addAll(size, c);
 }
//index參數(shù)指定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,如果刪除節(jié)點超過一代就會釋放內(nèi)存,因為有一個可用的迭代器。
  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));
 }

三、總結(jié)

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

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論

海林市| 建昌县| 庆元县| 仁怀市| 海兴县| 古丈县| 金昌市| 浑源县| 图们市| 信宜市| 易门县| 长治县| 昔阳县| 大埔县| 汶上县| 黄平县| 兴业县| 临高县| 垦利县| 湘阴县| 福安市| 临汾市| 肃南| 平利县| 台山市| 望城县| 龙南县| 固阳县| 贺兰县| 荣成市| 棋牌| 福泉市| 卫辉市| 新巴尔虎左旗| 固安县| 大姚县| 怀宁县| 革吉县| 抚州市| 徐水县| 丹棱县|