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

學(xué)習(xí)Java中的List集合

 更新時間:2022年02月07日 10:34:26   作者:Faith_xzc ?  
這篇文章主要介紹了學(xué)習(xí)Java中的List集合,List是一個有序集合, 說是集合,其實(shí)只是只是Collection接口的一個子接口,下面關(guān)于List的相關(guān)資料 需要的小伙伴可以參考一下,希望對你有所幫助

1.概述

List是一個有序集合(也被稱為序列)。此接口的用戶在列表中的每個元素都被插入的地方有精確的控制。用戶可以通過它們的整數(shù)索引(在列表中的位置)訪問元素,并在列表中搜索元素。 說是List集合,其實(shí)只是習(xí)慣說法,因?yàn)樗荂ollection接口的一個子接口(Collection有很多的子接口,這是其中三個主要的子接口之一,另外兩個后面都會說到),所以Collection接口中定義的方法在List接口中也是可以使用的,另外還根據(jù)List的特點(diǎn),又引入了其他的方法。

List接口的特點(diǎn):

  • 元素是以一種線性方式進(jìn)行存儲的
  • 元素存取有序,即元素的存入順序和取出順序一致。
  • 元素帶有索引,通過索引就可以精確的操作集合中的元素(與數(shù)組類似)
  • 元素可以重復(fù),通過元素的equals方法,來比較是否為重復(fù)的元素

2.List的使用

2.1List的常用方法

基本介紹:

這里說的常用方法是指除了實(shí)現(xiàn)Collection接口之外的。前面說到List集合中的元素是可以通過索引來操作集合中的元素的,所以List 集合里添加了一些根據(jù)索引來操作集合元素的方法。下面對這些方法進(jìn)行簡單

介紹:

  • void add(iint index, E element): 在列表中指定的位置上插入指定的元素
  • boolean addAll(int index, Collection c): 將指定的集合中的所有元素插入到指定位置的列表中
  • E get(int index):返回此列表中指定位置的元素
  • List subList(int fromIndex, int toIndex):返回List中一部分對象的集合,即返回的集合是List的子集合,并是以下標(biāo)索引取值。父集合List以fromIndex開始(包含),到toIndex結(jié)束(不包含)的 部分為返回的子集合
  • int indexOf(Object obj):返回此列表中指定元素的第一個出現(xiàn)的索引,如果此列表不包含元素,返回- 1
  • int lastIndexOf(Object obj):返回此列表中指定元素的最后一個發(fā)生的索引,如果此列表不包含元素,返回- 1
  • E remove(int index):移除此列表中指定位置的元素
  • E set(int index, E element):用指定元素替換此列表中指定位置的元素

代碼示例:

public class ListDemo {
? ? public static void main(String[] args) {
?? ??? ?// 通過List的實(shí)現(xiàn)類ArrayList創(chuàng)建List集合對象
? ? ?? ?List<String> list = new ArrayList<String>();

? ? ?? ?// 指定位置添加元素
? ? ?? ?list.add(0,"jack");
? ? ?? ?list.add(1,"rose");?? ?
? ? ?? ?list.add(2,"marry");?? ??? ?
? ? ?? ?System.out.println(list);
? ? ?? ?
? ? ?? ?// 刪除索引位置為2的元素?
? ? ?? ?list.remove(2); ? ??? ?
? ? ?? ?System.out.println(list);
? ? ?? ?
? ? ?? ?// 指定元素替換此列表中指定位置的元素
? ? ?? ?list.set(0, "老王");
? ? ?? ?System.out.println(list);
? ? ?? ?
? ? ?? ?// 獲取指定位置元素(也遍歷輸出下)?? ?
? ? ?? ?for(int i = 0;i<list.size();i++){
? ? ?? ??? ?System.out.println(list.get(i));
? ? ?? ?}
? ? ?? ?//還可以使用增強(qiáng)for
? ? ?? ?for (String string : list) {
?? ??? ??? ?System.out.println(string);
?? ??? ?} ??? ?
?? ?}
}

3.List的實(shí)現(xiàn)類

作為一個接口,List的實(shí)現(xiàn)類才是我們創(chuàng)建對象時候使用的(上面代碼示例里面用到了ArrayList實(shí)現(xiàn)類)。在List接口里,有三個常用的實(shí)現(xiàn)類:ArrayList、Vector、LinkedList。下面從源碼中分析和介紹它們。

3.1ArrayList

ArrayList底層通過數(shù)組實(shí)現(xiàn),ArrayList可以隨著元素的增加而動態(tài)擴(kuò)容。它是一個數(shù)組隊(duì)列,是Java集合框架中使用最多的一個類,但是它是線程不安全的。

  • 特點(diǎn):以數(shù)組的形式進(jìn)行存儲,因此隨機(jī)訪問速度較快,所有它適用于查詢
  • 缺點(diǎn):不適用于插入和刪除的操作 因?yàn)槊看尾僮鞫夹枰苿訑?shù)組中的元素;線程不安全

下面看下ArrayList的源碼:

public class ArrayList<E> extends AbstractList<E>
? ? ? ? implements List<E>, RandomAccess, Cloneable, java.io.Serializable{
? ? /**
? ? ?* Default initial capacity. 初始化的時候如果沒有指定長度的話,使用默認(rèn)長度10
? ? ?*/
? ? private static final int DEFAULT_CAPACITY = 10;
? ?/**
? ? ?* Shared empty array instance used for empty instances. 空數(shù)組
? ? ?*/
? ? private static final Object[] EMPTY_ELEMENTDATA = {};
? /**
? ? ?* Shared empty array instance used for default sized empty instances. We
? ? ?* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
? ? ?* first element is added. ?空數(shù)組
? ? ?*/
? ? private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
? ? ?* Constructs an empty list with an initial capacity of ten.構(gòu)造一個初始容量為10
? ? ?*/
? ? public ArrayList() {
? ? ? ? this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;//初始化為空數(shù)組
? ? }


? ? public boolean add(E e) {
? ? ? ? //查看當(dāng)前數(shù)組是否夠多存一個元素
? ? ? ? ensureCapacityInternal(size + 1); ?// Increments modCount!!
? ? ? ??
? ? ? ? //存入新元素到[size]位置,然后size自增1
? ? ? ? elementData[size++] = e;
? ? ? ? return true;
? ? }
? ?
? ?private void ensureCapacityInternal(int minCapacity) {
? ? ? ? ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
? ? }

? ?private static int calculateCapacity(Object[] elementData, int minCapacity) {
? ?? ??? ? //如果當(dāng)前數(shù)組還是空數(shù)組
? ? ? ? if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
? ? ? ? //那么minCapacity取DEFAULT_CAPACITY與minCapacity的最大值
? ? ? ? ? ? return Math.max(DEFAULT_CAPACITY, minCapacity);
? ? ? ? }
? ? ? ? //查看是否需要擴(kuò)容
? ? ? ? return minCapacity;
? ? ? }
? ??
? ? private void ensureExplicitCapacity(int minCapacity) {
? ? ? ? modCount++;//修改次數(shù)加1

? ? ? ? // 如果需要的最小容量比當(dāng)前數(shù)組的長度大,即當(dāng)前數(shù)組不夠存,就擴(kuò)容
? ? ? ? if (minCapacity - elementData.length > 0)
? ? ? ? ? ? grow(minCapacity);
? ? }
? ? private void grow(int minCapacity) {
? ? ? ? // overflow-conscious code
? ? ? ? int oldCapacity = elementData.length;//當(dāng)前數(shù)組容量
? ? ? ? int newCapacity = oldCapacity + (oldCapacity >> 1);//新數(shù)組容量是舊數(shù)組容量的1.5倍
? ? ? ? //看舊數(shù)組的1.5倍是否夠
? ? ? ? if (newCapacity - minCapacity < 0)
? ? ? ? ? ? newCapacity = minCapacity;
? ? ? ? //看舊數(shù)組的1.5倍是否超過最大數(shù)組限制
? ? ? ? if (newCapacity - MAX_ARRAY_SIZE > 0)
? ? ? ? ? ? newCapacity = hugeCapacity(minCapacity);
? ? ? ??
? ? ? ? //復(fù)制一個新數(shù)組
? ? ? ? elementData = Arrays.copyOf(elementData, newCapacity);
? ?? ?}
? ?? ?public boolean remove(Object o) {
? ? ? ? //先找到o在當(dāng)前ArrayList的數(shù)組中的下標(biāo)
? ? ? ? //分o是否為空兩種情況討論
? ? ? ? if (o == null) {
? ? ? ? ? ? for (int index = 0; index < size; index++)
? ? ? ? ? ? ? ? if (elementData[index] == null) {//null值用==比較
? ? ? ? ? ? ? ? ? ? fastRemove(index);
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? for (int index = 0; index < size; index++)
? ? ? ? ? ? ? ? if (o.equals(elementData[index])) {//非null值用equals比較
? ? ? ? ? ? ? ? ? ? fastRemove(index);
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false;
? ? }
? ? private void fastRemove(int index) {
? ? ? ? modCount++;//修改次數(shù)加1
? ? ? ? //需要移動的元素個數(shù)
? ? ? ? int numMoved = size - index - 1;
? ? ? ??
? ? ? ? //如果需要移動元素,就用System.arraycopy移動元素
? ? ? ? if (numMoved > 0)
? ? ? ? ? ? System.arraycopy(elementData, index+1, elementData, index,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?numMoved);
? ? ? ??
? ? ? ? //將elementData[size-1]位置置空,讓GC回收空間,元素個數(shù)減少
? ? ? ? elementData[--size] = null; // clear to let GC do its work
? ? }
? ? public E remove(int index) {
? ? ? ? rangeCheck(index);//檢驗(yàn)index是否合法

? ? ? ? modCount++;//修改次數(shù)加1
? ? ? ??
? ? ? ? //取出[index]位置的元素,[index]位置的元素就是要被刪除的元素,用于最后返回被刪除的元素
? ? ? ? E oldValue = elementData(index);
? ? ? ??
?? ??? ?//需要移動的元素個數(shù)
? ? ? ? int numMoved = size - index - 1;
? ? ? ??
? ? ? ? //如果需要移動元素,就用System.arraycopy移動元素
? ? ? ? if (numMoved > 0)
? ? ? ? ? ? System.arraycopy(elementData, index+1, elementData, index,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?numMoved);
? ? ? ? //將elementData[size-1]位置置空,讓GC回收空間,元素個數(shù)減少
? ? ? ? elementData[--size] = null; // clear to let GC do its work

? ? ? ? return oldValue;
? ? }
? ? public E set(int index, E element) {
? ? ? ? rangeCheck(index);//檢驗(yàn)index是否合法

? ? ? ? //取出[index]位置的元素,[index]位置的元素就是要被替換的元素,用于最后返回被替換的元素
? ? ? ? E oldValue = elementData(index);
? ? ? ? //用element替換[index]位置的元素
? ? ? ? elementData[index] = element;
? ? ? ? return oldValue;
? ? }
? ? public E get(int index) {
? ? ? ? rangeCheck(index);//檢驗(yàn)index是否合法

? ? ? ? return elementData(index);//返回[index]位置的元素
? ? }
? ? ?public int indexOf(Object o) {
? ? ? ? //分為o是否為空兩種情況
? ? ? ? if (o == null) {
? ? ? ? ? ? //從前往后找
? ? ? ? ? ? for (int i = 0; i < size; i++)
? ? ? ? ? ? ? ? if (elementData[i]==null)
? ? ? ? ? ? ? ? ? ? return i;
? ? ? ? } else {
? ? ? ? ? ? for (int i = 0; i < size; i++)
? ? ? ? ? ? ? ? if (o.equals(elementData[i]))
? ? ? ? ? ? ? ? ? ? return i;
? ? ? ? }
? ? ? ? return -1;
? ? }
? ? public int lastIndexOf(Object o) {
? ? ? ? ?//分為o是否為空兩種情況
? ? ? ? if (o == null) {
? ? ? ? ? ? //從后往前找
? ? ? ? ? ? for (int i = size-1; i >= 0; i--)
? ? ? ? ? ? ? ? if (elementData[i]==null)
? ? ? ? ? ? ? ? ? ? return i;
? ? ? ? } else {
? ? ? ? ? ? for (int i = size-1; i >= 0; i--)
? ? ? ? ? ? ? ? if (o.equals(elementData[i]))
? ? ? ? ? ? ? ? ? ? return i;
? ? ? ? }
? ? ? ? return -1;
? ? }

從上面的源碼中我們可以看到:

  • ArrayList 在初始化的時候如果我們沒有指定長度的話,它會有一個默認(rèn)長度10,每次擴(kuò)容的時候?yàn)樵黾?.5倍
  • 然后是ArrayList 的一些常見的方法的源碼介紹

3.2Vector

Vector的底層也是通過數(shù)組實(shí)現(xiàn),方法與ArrayList基本一致,。但是Vector是線程安全的. 這是因?yàn)槠浼由狭?synchronized 關(guān)鍵字, 用來保證線程安全。

  • 優(yōu)點(diǎn): 以數(shù)組的形式進(jìn)行存儲,因此隨機(jī)訪問速度較快,所有它適用于查詢;線程安全
  • 缺點(diǎn): 不適用于插入和刪除的操作 因?yàn)槊看尾僮鞫夹枰苿訑?shù)組中的元素

下面看下Vector的源碼:

? /**
? ? ?* Constructs an empty vector so that its internal data array
? ? ?* has size {@code 10} and its standard capacity increment is
? ? ?* zero.
? ? ?*/
? ? public Vector() {
? ? ? ? this(10); //指定初始容量initialCapacity為10
? ? }
? ? public Vector(int initialCapacity) {
? ? ? ? this(initialCapacity, 0);//指定capacityIncrement增量為0
? ? }
? ? public Vector(int initialCapacity, int capacityIncrement增量為0) {
? ? ? ? super();
? ? ? ? //判斷了形參初始容量initialCapacity的合法性
? ? ? ? if (initialCapacity < 0)
? ? ? ? ? ? throw new IllegalArgumentException("Illegal Capacity: "+
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?initialCapacity);
? ? ? ? //創(chuàng)建了一個Object[]類型的數(shù)組
? ? ? ? this.elementData = new Object[initialCapacity];//默認(rèn)是10
? ? ? ? //增量,默認(rèn)是0,如果是0,后面就按照2倍增加,如果不是0,后面就按照你指定的增量進(jìn)行增量
? ? ? ? this.capacityIncrement = capacityIncrement;
? ? }
? ? //synchronized意味著線程安全的 ??
?? ?public synchronized boolean add(E e) {
? ? ? ? modCount++;
? ? ?? ?//看是否需要擴(kuò)容
? ? ? ? ensureCapacityHelper(elementCount + 1);
? ? ?? ?//把新的元素存入[elementCount],存入后,elementCount元素的個數(shù)增1
? ? ? ? elementData[elementCount++] = e;
? ? ? ? return true;
? ? }

? ? private void ensureCapacityHelper(int minCapacity) {
? ? ? ? // overflow-conscious code
? ? ? ? //看是否超過了當(dāng)前數(shù)組的容量
? ? ? ? if (minCapacity - elementData.length > 0)
? ? ? ? ? ? grow(minCapacity);//擴(kuò)容
? ? }
? ? private void grow(int minCapacity) {
? ? ? ? // overflow-conscious code
? ? ? ? int oldCapacity = elementData.length;//獲取目前數(shù)組的長度
? ? ? ? //如果capacityIncrement增量是0,新容量 = oldCapacity的2倍
? ? ? ? //如果capacityIncrement增量是不是0,新容量 = oldCapacity + capacityIncrement增量;
? ? ? ? int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?capacityIncrement : oldCapacity);
? ? ? ??
? ? ? ? //如果按照上面計算的新容量還不夠,就按照你指定的需要的最小容量來擴(kuò)容minCapacity
? ? ? ? if (newCapacity - minCapacity < 0)
? ? ? ? ? ? newCapacity = minCapacity;
? ? ? ??
? ? ? ? //如果新容量超過了最大數(shù)組限制,那么單獨(dú)處理
? ? ? ? if (newCapacity - MAX_ARRAY_SIZE > 0)
? ? ? ? ? ? newCapacity = hugeCapacity(minCapacity);
? ? ? ??
? ? ? ? //把舊數(shù)組中的數(shù)據(jù)復(fù)制到新數(shù)組中,新數(shù)組的長度為newCapacity
? ? ? ? elementData = Arrays.copyOf(elementData, newCapacity);
? ? }
? ? ?public boolean remove(Object o) {
? ? ? ? return removeElement(o);
? ? }
? ? public synchronized boolean removeElement(Object obj) {
? ? ? ? modCount++;
? ? ? ? //查找obj在當(dāng)前Vector中的下標(biāo)
? ? ? ? int i = indexOf(obj);
? ? ? ? //如果i>=0,說明存在,刪除[i]位置的元素
? ? ? ? if (i >= 0) {
? ? ? ? ? ? removeElementAt(i);
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? return false;
? ? }
? ? public int indexOf(Object o) {
? ? ? ? return indexOf(o, 0);
? ? }
? ? public synchronized int indexOf(Object o, int index) {
? ? ? ? if (o == null) {//要查找的元素是null值
? ? ? ? ? ? for (int i = index ; i < elementCount ; i++)
? ? ? ? ? ? ? ? if (elementData[i]==null)//如果是null值,用==null判斷
? ? ? ? ? ? ? ? ? ? return i;
? ? ? ? } else {//要查找的元素是非null值
? ? ? ? ? ? for (int i = index ; i < elementCount ; i++)
? ? ? ? ? ? ? ? if (o.equals(elementData[i]))//如果是非null值,用equals判斷
? ? ? ? ? ? ? ? ? ? return i;
? ? ? ? }
? ? ? ? return -1;
? ? }
? ? public synchronized void removeElementAt(int index) {
? ? ? ? modCount++;
? ? ? ? //判斷下標(biāo)的合法性
? ? ? ? if (index >= elementCount) {
? ? ? ? ? ? throw new ArrayIndexOutOfBoundsException(index + " >= " +
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?elementCount);
? ? ? ? }
? ? ? ? else if (index < 0) {
? ? ? ? ? ? throw new ArrayIndexOutOfBoundsException(index);
? ? ? ? }
? ? ? ??
? ? ? ? //j是要移動的元素的個數(shù)
? ? ? ? int j = elementCount - index - 1;
? ? ? ? //如果需要移動元素,就調(diào)用System.arraycopy進(jìn)行移動
? ? ? ? if (j > 0) {
? ? ? ? ? ? //把index+1位置以及后面的元素往前移動
? ? ? ? ? ? //index+1的位置的元素移動到index位置,依次類推
? ? ? ? ? ? //一共移動j個
? ? ? ? ? ? System.arraycopy(elementData, index + 1, elementData, index, j);
? ? ? ? }
? ? ? ? //元素的總個數(shù)減少
? ? ? ? elementCount--;
? ? ? ? //將elementData[elementCount]這個位置置空,用來添加新元素,位置的元素等著被GC回收
? ? ? ? elementData[elementCount] = null; /* to let gc do its work */
? ? }

從上面的源碼中我們可以看到:

Vector在初始化的時候如果我們沒有指定長度的話,它會有一個默認(rèn)長度10,每次擴(kuò)容的時候?yàn)樵黾?倍
然后是Vector的一些常見的方法的源碼介紹

3.3LinkedList

LinkedList底層的數(shù)據(jù)存儲結(jié)構(gòu)是鏈表結(jié)構(gòu),而且還是一個雙向鏈表,可以實(shí)現(xiàn)雙向操作。此外,LinkedList還實(shí)現(xiàn)了棧和隊(duì)列的操作方法,因此也可以作為棧、隊(duì)列和雙端隊(duì)列來使用,如peek 、push、pop等方法。

  • 優(yōu)點(diǎn): 以鏈表形式進(jìn)行存儲,因此隨機(jī)訪問速度查詢慢,增刪快。
  • 缺點(diǎn): 線程不安全

下面看一下源碼:

int size = 0;
Node<E> first;//記錄第一個結(jié)點(diǎn)的位置
Node<E> last;//記錄最后一個結(jié)點(diǎn)的位置

? ? private static class Node<E> {
? ? ? ? E item;//元素數(shù)據(jù)
? ? ? ? Node<E> next;//下一個結(jié)點(diǎn)
? ? ? ? Node<E> prev;//前一個結(jié)點(diǎn)

? ? ? ? Node(Node<E> prev, E element, Node<E> next) {
? ? ? ? ? ? this.item = element;
? ? ? ? ? ? this.next = next;
? ? ? ? ? ? this.prev = prev;
? ? ? ? }
? ? }
? ? ? public boolean add(E e) {
? ? ? ? linkLast(e);//默認(rèn)把新元素鏈接到鏈表尾部
? ? ? ? return true;
? ? }
? ? void linkLast(E e) {
? ? ? ? final Node<E> l = last;//用l 記錄原來的最后一個結(jié)點(diǎn)
? ? ? ??
? ? ? ? //創(chuàng)建新結(jié)點(diǎn)
? ? ? ? final Node<E> newNode = new Node<>(l, e, null);
? ? ? ? //現(xiàn)在的新結(jié)點(diǎn)是最后一個結(jié)點(diǎn)了
? ? ? ? last = newNode;
? ? ? ??
? ? ? ? //如果l==null,說明原來的鏈表是空的
? ? ? ? if (l == null)
? ? ? ? ? ? //那么新結(jié)點(diǎn)同時也是第一個結(jié)點(diǎn)
? ? ? ? ? ? first = newNode;
? ? ? ? else
? ? ? ? ? ? //否則把新結(jié)點(diǎn)鏈接到原來的最后一個結(jié)點(diǎn)的next中
? ? ? ? ? ? l.next = newNode;
? ? ? ? //元素個數(shù)增加
? ? ? ? size++;
? ? ? ? //修改次數(shù)增加
? ? ? ? modCount++;
? ? }
? ? ?public boolean remove(Object o) {
? ? ? ? //分o是否為空兩種情況
? ? ? ? if (o == null) {
? ? ? ? ? ? //找到o對應(yīng)的結(jié)點(diǎn)x
? ? ? ? ? ? for (Node<E> x = first; x != null; x = x.next) {
? ? ? ? ? ? ? ? if (x.item == null) {
? ? ? ? ? ? ? ? ? ? unlink(x);//刪除x結(jié)點(diǎn)
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? //找到o對應(yīng)的結(jié)點(diǎn)x
? ? ? ? ? ? for (Node<E> x = first; x != null; x = x.next) {
? ? ? ? ? ? ? ? if (o.equals(x.item)) {
? ? ? ? ? ? ? ? ? ? unlink(x);//刪除x結(jié)點(diǎn)
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return false;
? ? }
? ? E unlink(Node<E> x) {//x是要被刪除的結(jié)點(diǎn)
? ? ? ? // assert x != null;
? ? ? ? final E element = x.item;//被刪除結(jié)點(diǎn)的數(shù)據(jù)
? ? ? ? final Node<E> next = x.next;//被刪除結(jié)點(diǎn)的下一個結(jié)點(diǎn)
? ? ? ? final Node<E> prev = x.prev;//被刪除結(jié)點(diǎn)的上一個結(jié)點(diǎn)

? ? ? ? //如果被刪除結(jié)點(diǎn)的前面沒有結(jié)點(diǎn),說明被刪除結(jié)點(diǎn)是第一個結(jié)點(diǎn)
? ? ? ? if (prev == null) {
? ? ? ? ? ? //那么被刪除結(jié)點(diǎn)的下一個結(jié)點(diǎn)變?yōu)榈谝粋€結(jié)點(diǎn)
? ? ? ? ? ? first = next;
? ? ? ? } else {//被刪除結(jié)點(diǎn)不是第一個結(jié)點(diǎn)
? ? ? ? ? ? //被刪除結(jié)點(diǎn)的上一個結(jié)點(diǎn)的next指向被刪除結(jié)點(diǎn)的下一個結(jié)點(diǎn)
? ? ? ? ? ? prev.next = next;
? ? ? ? ? ? //斷開被刪除結(jié)點(diǎn)與上一個結(jié)點(diǎn)的鏈接
? ? ? ? ? ? x.prev = null;//使得GC回收
? ? ? ? }

? ? ? ? //如果被刪除結(jié)點(diǎn)的后面沒有結(jié)點(diǎn),說明被刪除結(jié)點(diǎn)是最后一個結(jié)點(diǎn)
? ? ? ? if (next == null) {
? ? ? ? ? ? //那么被刪除結(jié)點(diǎn)的上一個結(jié)點(diǎn)變?yōu)樽詈笠粋€結(jié)點(diǎn)
? ? ? ? ? ? last = prev;
? ? ? ? } else {//被刪除結(jié)點(diǎn)不是最后一個結(jié)點(diǎn)
? ? ? ? ? ? //被刪除結(jié)點(diǎn)的下一個結(jié)點(diǎn)的prev執(zhí)行被刪除結(jié)點(diǎn)的上一個結(jié)點(diǎn)
? ? ? ? ? ? next.prev = prev;
? ? ? ? ? ? //斷開被刪除結(jié)點(diǎn)與下一個結(jié)點(diǎn)的連接
? ? ? ? ? ? x.next = null;//使得GC回收
? ? ? ? }
?? ??? ?//把被刪除結(jié)點(diǎn)的數(shù)據(jù)也置空,使得GC回收
? ? ? ? x.item = null;
? ? ? ? //元素個數(shù)減少
? ? ? ? size--;
? ? ? ? //修改次數(shù)增加
? ? ? ? modCount++;
? ? ? ? //返回被刪除結(jié)點(diǎn)的數(shù)據(jù)
? ? ? ? return element;
? ? }

從上面的源碼中我們可以看到:

LinkedList是基于鏈表的,所以沒有擴(kuò)容方法,默認(rèn)加入元素是尾部自動擴(kuò)容
然后是LinkedList的一些常見的方法的源碼介紹

3.4ArrayList與Vector的區(qū)別

它們的底層結(jié)構(gòu)都是數(shù)組,我們稱為動態(tài)數(shù)組。

  • ArrayList是新版的動態(tài)數(shù)組,線程不安全,效率高,Vector是舊版的動態(tài)數(shù)組,線程安全,效率低。
  • 動態(tài)數(shù)組的擴(kuò)容機(jī)制不同,ArrayList擴(kuò)容為原來的1.5倍,Vector擴(kuò)容增加為原來的2倍。
  • 數(shù)組的初始化容量,如果在構(gòu)建ArrayList與Vector的集合對象時,沒有顯式指定初始化容量,那么Vector的內(nèi)部數(shù)組的初始容量默認(rèn)為10,而ArrayList在JDK1.6及之前的版本也是10,而JDK1.7之后的版本ArrayList初始化為長度為0的空數(shù)組,之后在添加第一個元素時,再創(chuàng)建長度為10的數(shù)組。

到此這篇關(guān)于學(xué)習(xí)Java中的List集合的文章就介紹到這了,更多相關(guān)Java中的List集合內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • idea中打開項(xiàng)目時import project和open區(qū)別詳解

    idea中打開項(xiàng)目時import project和open區(qū)別詳解

    本文主要介紹了idea中打開項(xiàng)目時import project和open區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 如何應(yīng)對spring框架的HTTP ERROR 400 Bad Request錯誤返回問題

    如何應(yīng)對spring框架的HTTP ERROR 400 Bad Request錯

    這篇文章主要介紹了如何應(yīng)對spring框架的HTTP ERROR 400 Bad Request錯誤返回問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 詳解如何繼承Mybatis中Mapper.xml文件

    詳解如何繼承Mybatis中Mapper.xml文件

    這篇文章主要為大家介紹了詳解如何繼承Mybatis中Mapper.xml文件,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Spring?MVC的三種異常處理方式實(shí)例詳解

    Spring?MVC的三種異常處理方式實(shí)例詳解

    在SpringMVC 中,不管是編譯異常還是運(yùn)行時異常,都可以最終由 SpringMVC提供的異常處理器進(jìn)行統(tǒng)一處理,這樣就避免了隨時隨地捕獲處理的繁瑣性,這篇文章主要介紹了Spring?MVC的三種異常處理方式?,需要的朋友可以參考下
    2024-01-01
  • Springboot安全框架整合SpringSecurity實(shí)現(xiàn)方式

    Springboot安全框架整合SpringSecurity實(shí)現(xiàn)方式

    這篇文章主要介紹了Spring全家桶中Springboot安全框架整合SpringSecurity的實(shí)現(xiàn)方式,有需要的朋友可以借鑒參考下,希望可以有所幫助
    2021-09-09
  • java8中forkjoin和optional框架使用

    java8中forkjoin和optional框架使用

    這篇文章主要介紹了java8中forkjoin和optional框架使用心得以及用法講解,需要的朋友參考下吧。
    2017-12-12
  • Java使用TCP協(xié)議發(fā)送和接收數(shù)據(jù)方式

    Java使用TCP協(xié)議發(fā)送和接收數(shù)據(jù)方式

    這篇文章詳細(xì)介紹了Java中使用TCP進(jìn)行數(shù)據(jù)傳輸?shù)牟襟E,包括創(chuàng)建Socket對象、獲取輸入輸出流、讀寫數(shù)據(jù)以及釋放資源,通過兩個示例代碼TCPTest01.java和TCPTest02.java,展示了如何在客戶端和服務(wù)器端進(jìn)行數(shù)據(jù)交換
    2024-12-12
  • Java異常處理機(jī)制throws舉例詳解

    Java異常處理機(jī)制throws舉例詳解

    這篇文章主要介紹了Java中異常處理機(jī)制,包括finally的使用、異步處理的方式(throws)、以及手動拋出異常,finally用于確保資源釋放,throws用于聲明方法可能拋出的異常,由調(diào)用者處理,手動拋出異常則是在特定條件下主動拋出異常對象,需要的朋友可以參考下
    2024-11-11
  • Spring?boot讀取外部化配置的方法

    Spring?boot讀取外部化配置的方法

    大家好,本篇文章主要講的是Spring?boot讀取外部化配置的方法,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Spring Controller接收前端JSON數(shù)據(jù)請求方式

    Spring Controller接收前端JSON數(shù)據(jù)請求方式

    這篇文章主要為大家介紹了Spring Controller接收前端JSON數(shù)據(jù)請求方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07

最新評論

莒南县| 本溪| 根河市| 收藏| 荣成市| 武隆县| 加查县| 大同市| 万全县| 顺昌县| 屯门区| 濮阳县| 高台县| 林西县| 新安县| 闻喜县| 孝昌县| 昭平县| 百色市| 会宁县| 承德县| 瑞丽市| 元江| 白水县| 金川县| 尉犁县| 长汀县| 安庆市| 青田县| 固安县| 延庆县| 右玉县| 盐津县| 苍山县| 西吉县| 桐庐县| 荥阳市| 昌黎县| 高雄市| 陵水| 东兴市|