十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
前面一章節(jié),我們介紹了集合的類圖,那么本節(jié)將學(xué)習(xí)Collection 接口中最常用的子類ArrayList類,本章分為下面幾部分講解(說明本章采用的JDK1.6源碼進(jìn)行分析,因?yàn)閭€人認(rèn)為雖然JDK1.8進(jìn)行了部分改動,但萬變不離其宗,仍然采用的JDK1.6的引子進(jìn)行的優(yōu)化,因此學(xué)會了1.6對于1.8也就理解了)。
創(chuàng)新互聯(lián)建站專注于繁峙企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),購物商城網(wǎng)站建設(shè)。繁峙網(wǎng)站建設(shè)公司,為繁峙等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
一、ArrayList 的常見功能
在分析ArrayList的源碼前,我們先看下ArrayList的常見的功能:
package study.collection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class TestDemo01
{
public static void main(String[] args)
{
List list = new ArrayList();
//ArrayList:底層實(shí)現(xiàn)時數(shù)組,線程不安全,效率高。所以,查詢快。修改、插入、刪除慢。
//LinkedList:底層實(shí)現(xiàn)是鏈表,線程不安全,效率高。所以,查詢慢。修改、插入、刪除快。
//Vector:線程安全的,效率低。
list.add("aaa");
list.add("aaa");
list.add(new Date());
list.add(new Dog());
list.add(1234); //注意,list集合中只能添加引用類型,這里包裝類的:自動裝箱!
list.remove(new String("aaa"));
System.out.println(list.size());
for(int i=0;i從上述可以看到了,ArrayList 接口中除了繼承自父類Collection 接口中的方法外,還實(shí)現(xiàn)了List接口中擴(kuò)充的和索引相關(guān)的方法,這都源于其底層為數(shù)組結(jié)構(gòu)。
二、ArrayList 的重要的屬性
上面的部分列舉了ArrayList中常見的一些功能方法,那么這些方法又是如何使用的呢,下面我們將進(jìn)行源碼的剖析,在剖析前,我們可以自己思考下,我們知道ArrayList 是一個動態(tài)擴(kuò)展的集合,之所以動態(tài)擴(kuò)展的原因或者說比數(shù)組強(qiáng)的地方肯定就在于數(shù)組的長度是固定的,不能擴(kuò)展,這是數(shù)組的最大缺陷,所以才有了集合,那么ArrayList,那么其底層肯定也采用的是數(shù)組結(jié)構(gòu),因?yàn)樗蠥rrayList嘛,那么其重要的屬性之一,必然是定義了一個數(shù)組。如下:
public class ArrayListextends AbstractList implements List , RandomAccess, Cloneable, java.io.Serializable { private static final long serialVersionUID = 8683452581122892189L; /** * The array buffer into which the elements of the ArrayList are stored. * The capacity of the ArrayList is the length of this array buffer. */ private transient Object[] elementData; /** * The size of the ArrayList (the number of elements it contains). * * @serial */ private int size;
ArrayList就是一個以數(shù)組形式實(shí)現(xiàn)的集合,其元素的功能為:
private transient Object[] elementData; //ArrayList是基于數(shù)組的一個實(shí)現(xiàn),elementData就是底層的數(shù)組
private int size; //ArrayList里面元素的個數(shù),這里要注意一下,size是按照調(diào)用add、remove方法的次數(shù)進(jìn)行自增或者自減的,所以add了一個null進(jìn)入ArrayList,size也會加1
三、ArrayList 的構(gòu)造方法分析
在分析完上面的屬性后,我們緊接著來看下ArrayList的構(gòu)造方法:
/**
*構(gòu)造一個具有指定容量的list
*/
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
/**
* 構(gòu)造一個初始容量為10的list,也就說當(dāng)我們經(jīng)常采用new ArrayList()的時候,實(shí)際分配的大小就為10
*/
public ArrayList() {
this(10);
}
/**
*構(gòu)造一個包含指定元素的list,這些元素的是按照Collection的迭代器返回的順序排列的
*/
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652) 這里是因?yàn)閠oArray可能不一定是Object類型的,因此判斷如果不是就進(jìn)行了拷貝轉(zhuǎn)換操作
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
可以看到,ArrayList 提供了三個構(gòu)造方法,分別的含義,已經(jīng)注釋到代碼上面了,那么想一下指定容量的構(gòu)造方法的意義,既然默認(rèn)為10就可以那么為什么還要提供一個可以指定容量大小的構(gòu)造方法呢?思考下,下面會說到。
四、ArrayList 的常見方法分析
1.add 添加元素

添加的方法,共有四個,下面我們分別分析下其功能源碼:
/**
*添加一個元素
*/
public boolean add(E e)
{
// 進(jìn)行擴(kuò)容檢查
ensureCapacity(size + 1); // Increments modCount!!
//將e增加至list的數(shù)據(jù)尾部,容量+1
elementData[size++] = e;
return true;
}
/**
*在指定位置添加一個元素
*/
public void add(int index, E element) {
// 判斷索引是否越界
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
// 進(jìn)行擴(kuò)容檢查
ensureCapacity(size+1); // Increments modCount!!
// 對數(shù)組進(jìn)行復(fù)制處理,目的就是空出index的位置插入element,并將index后的元素位移一個位置
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
// 將指定的index位置賦值為element
elementData[index] = element;
// list容量+1
size++;
}
/**
*增加一個集合元素
*/
public boolean addAll(Collection<? extends E> c) {
//將c轉(zhuǎn)換為數(shù)組
Object[] a = c.toArray();
int numNew = a.length;
//擴(kuò)容檢查
ensureCapacity(size + numNew); // Increments modCount
//將c添加至list的數(shù)據(jù)尾部
System.arraycopy(a, 0, elementData, size, numNew);
//更新當(dāng)前容器大小
size += numNew;
return numNew != 0;
}
/**
* 在指定位置,增加一個集合元素
*/
public boolean addAll(int index, Collection<? extends E> c) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: " + index + ", Size: " + size);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew); // Increments modCount
// 計(jì)算需要移動的長度(index之后的元素個數(shù))
int numMoved = size - index;
// 數(shù)組復(fù)制,空出第index到index+numNum的位置,即將數(shù)組index后的元素向右移動numNum個位置
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew,
numMoved);
// 將要插入的集合元素復(fù)制到數(shù)組空出的位置中
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
/**
* 數(shù)組容量檢查,不夠時則進(jìn)行擴(kuò)容
*/
public void ensureCapacity( int minCapacity) {
modCount++;
// 當(dāng)前數(shù)組的長度
int oldCapacity = elementData.length;
// 最小需要的容量大于當(dāng)前數(shù)組的長度則進(jìn)行擴(kuò)容
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
// 新擴(kuò)容的數(shù)組長度為舊容量的1.5倍+1
int newCapacity = (oldCapacity * 3)/2 + 1;
// 如果新擴(kuò)容的數(shù)組長度還是比最小需要的容量小,則以最小需要的容量為長度進(jìn)行擴(kuò)容
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
// 進(jìn)行數(shù)據(jù)拷貝,Arrays.copyOf底層實(shí)現(xiàn)是System.arrayCopy()
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
2.remove 刪除
/**
* 根據(jù)索引位置刪除元素
*/
public E remove(int index) {
// 數(shù)組越界檢查
RangeCheck(index);
modCount++;
// 取出要刪除位置的元素,供返回使用
E oldValue = (E) elementData[index];
// 計(jì)算數(shù)組要復(fù)制的數(shù)量
int numMoved = size - index - 1;
// 數(shù)組復(fù)制,就是將index之后的元素往前移動一個位置
if (numMoved > 0)
System. arraycopy(elementData, index+1, elementData, index,
numMoved);
// 將數(shù)組最后一個元素置空(因?yàn)閯h除了一個元素,然后index后面的元素都向前移動了,所以最后一個就沒用了),好讓gc盡快回收
// 不要忘了size減一
elementData[--size ] = null; // Let gc do its work
return oldValue;
}
/**
* 根據(jù)元素內(nèi)容刪除,只刪除匹配的第一個
*/
public boolean remove(Object o) {
// 對要刪除的元素進(jìn)行null判斷
// 對數(shù)據(jù)元素進(jìn)行遍歷查找,知道找到第一個要刪除的元素,刪除后進(jìn)行返回,如果要刪除的元素正好是最后一個那就慘了,時間復(fù)雜度可達(dá)O(n) 。。。
if (o == null) {
for (int index = 0; index < size; index++)
// null值要用==比較
if (elementData [index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
// 非null當(dāng)然是用equals比較了
if (o.equals(elementData [index])) {
fastRemove(index);
return true;
}
}
return false;
}
/*
* Private remove method that skips bounds checking and does not
* return the value removed.
*/
private void fastRemove(int index) {
modCount++;
// 原理和之前的add一樣,還是進(jìn)行數(shù)組復(fù)制,將index后的元素向前移動一個位置,不細(xì)解釋了,
int numMoved = size - index - 1;
if (numMoved > 0)
System. arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size ] = null; // Let gc do its work
}
/**
* 數(shù)組越界檢查
*/
private void RangeCheck(int index) {
if (index >= size )
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: " +size);
}
分析:
增加和刪除方法到這里就解釋完了,代碼是很簡單,主要需要特別關(guān)心的就兩個地方:1.數(shù)組擴(kuò)容,2.數(shù)組復(fù)制,這兩個操作都是極費(fèi)效率的,最慘的情況下(添加到list第一個位置,刪除list最后一個元素或刪除list第一個索引位置的元素)時間復(fù)雜度可達(dá)O(n)。
還記得上面那個坑嗎(為什么提供一個可以指定容量大小的構(gòu)造方法 )?看到這里是不是有點(diǎn)明白了呢,簡單解釋下:如果數(shù)組初試容量過小,假設(shè)默認(rèn)的10個大小,而我們使用ArrayList的主要操作時增加元素,不斷的增加,一直增加,不停的增加,會出現(xiàn)上面后果?那就是數(shù)組容量不斷的受挑釁,數(shù)組需要不斷的進(jìn)行擴(kuò)容,擴(kuò)容的過程就是數(shù)組拷貝System.arraycopy的過程,每一次擴(kuò)容就會開辟一塊新的內(nèi)存空間和數(shù)據(jù)的復(fù)制移動,這樣勢必對性能造成影響。那么在這種以寫為主(寫會擴(kuò)容,刪不會縮容)場景下,提前預(yù)知性的設(shè)置一個大容量,便可減少擴(kuò)容的次數(shù),提高了性能。
數(shù)組擴(kuò)容伴隨著開辟新建的內(nèi)存空間以創(chuàng)建新數(shù)組然后進(jìn)行數(shù)據(jù)復(fù)制,而數(shù)組復(fù)制不需要開辟新內(nèi)存空間,只需將數(shù)據(jù)進(jìn)行復(fù)制。
上面講增加元素可能會進(jìn)行擴(kuò)容,而刪除元素卻不會進(jìn)行縮容,如果在已刪除為主的場景下使用list,一直不停的刪除而很少進(jìn)行增加,那么會出現(xiàn)什么情況?再或者數(shù)組進(jìn)行一次大擴(kuò)容后,我們后續(xù)只使用了幾個空間,會出現(xiàn)上面情況?當(dāng)然是空間浪費(fèi)啦啦啦,怎么辦呢?
/**
* 將底層數(shù)組的容量調(diào)整為當(dāng)前實(shí)際元素的大小,來釋放空間。
*/
public void trimToSize() {
modCount++;
// 當(dāng)前數(shù)組的容量
int oldCapacity = elementData .length;
// 如果當(dāng)前實(shí)際元素大小 小于 當(dāng)前數(shù)組的容量,則進(jìn)行縮容
if (size < oldCapacity) {
elementData = Arrays.copyOf( elementData, size );
}
3.更新 set
/**
* 將指定位置的元素更新為新元素
*/
public E set( int index, E element) {
// 數(shù)組越界檢查
RangeCheck(index);
// 取出要更新位置的元素,供返回使用
E oldValue = (E) elementData[index];
// 將該位置賦值為行的元素
elementData[index] = element;
// 返回舊元素
return oldValue;
}
4.查找
/**
* 查找指定位置上的元素
*/
public E get( int index) {
RangeCheck(index);
return (E) elementData [index];
}
由于ArrayList使用數(shù)組實(shí)現(xiàn),更新和查找直接基于下標(biāo)操作,變得十分簡單。
5.是否包含.
/**
* Returns true if this list contains the specified element.
* More formally, returns true if and only if this list contains
* at least one element e such that
* (o==null ? e==null : o.equals(e)).
*
* @param o element whose presence in this list is to be tested
* @return true if this list contains the specified element
*/
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
/**
* Returns the index of the first occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the lowest index i such that
* (o==null ? get(i)==null : o.equals(get(i))),
* or -1 if there is no such index.
*/
public int indexOf(Object 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;
}
/**
* Returns the index of the last occurrence of the specified element
* in this list, or -1 if this list does not contain the element.
* More formally, returns the highest index i such that
* (o==null ? get(i)==null : o.equals(get(i))),
* or -1 if there is no such index.
*/
public int lastIndexOf(Object 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;
}
contains主要是檢查indexOf,也就是元素在list中出現(xiàn)的索引位置也就是數(shù)組下標(biāo),再看indexOf和lastIndexOf代碼是不是很熟悉,沒錯,和public booleanremove(Object o) 的代碼一樣,都是元素null判斷,都是循環(huán)比較。
6.容量判斷
/**
* Returns the number of elements in this list.
*
* @return the number of elements in this list
*/
public int size() {
return size ;
}
/**
* Returns true if this list contains no elements.
*
* @return true if this list contains no elements
*/
public boolean isEmpty() {
return size == 0;
}
說明:modCount 是干什么的,怎么到處都在操作這個變量,還有遍歷呢,為啥不講?由于iterator遍歷相對比較復(fù)雜,而且iterator 是GoF經(jīng)典設(shè)計(jì)模式比較重要的一個,之后會對iterator單獨(dú)分析。
五、transient 分析 和 ArrayList的優(yōu)缺點(diǎn)
1.ArrayList的優(yōu)缺點(diǎn)
1、ArrayList底層以數(shù)組實(shí)現(xiàn),是一種隨機(jī)訪問模式,再加上它實(shí)現(xiàn)了RandomAccess接口,因此查找也就是get的時候非???/p>
2、ArrayList在順序添加一個元素的時候非常方便,只是往數(shù)組里面添加了一個元素而已
不過ArrayList的缺點(diǎn)也十分明顯:
1、刪除元素的時候,涉及到一次元素復(fù)制,如果要復(fù)制的元素很多,那么就會比較耗費(fèi)性能
2、插入元素的時候,涉及到一次元素復(fù)制,如果要復(fù)制的元素很多,那么就會比較耗費(fèi)性能
因此,ArrayList比較適合順序添加、隨機(jī)訪問的場景。
2.ArrayList和Vector的區(qū)別
ArrayList是線程非安全的,這很明顯,因?yàn)锳rrayList中所有的方法都不是同步的,在并發(fā)下一定會出現(xiàn)線程安全問題。那么我們想要使用ArrayList并且讓它線程安全怎么辦?一個方法是用Collections.synchronizedList方法把你的ArrayList變成一個線程安全的List,比如:
ListsynchronizedList = Collections.synchronizedList(list); synchronizedList.add("aaa"); synchronizedList.add("bbb"); for (int i = 0; i < synchronizedList.size(); i++) { System.out.println(synchronizedList.get(i)); }
另一個方法就是Vector,它是ArrayList的線程安全版本,其實(shí)現(xiàn)90%和ArrayList都完全一樣,區(qū)別在于:
1、Vector是線程安全的,ArrayList是線程非安全的
2、Vector可以指定增長因子,如果該增長因子指定了,那么擴(kuò)容的時候會每次新的數(shù)組大小會在原數(shù)組的大小基礎(chǔ)上加上增長因子;如果不指定增長因子,那么就給原數(shù)組大小
3.為什么ArrayList的elementData是用transient修飾的?
private transient Object[] elementData;
為什么elementData是使用transient修飾的呢?我們看一下ArrayList的定義:
public class ArrayListextends AbstractList implements List , RandomAccess, Cloneable, java.io.Serializable
看到ArrayList實(shí)現(xiàn)了Serializable接口,這意味著ArrayList是可以被序列化的,用transient修飾elementData意味著我不希望elementData數(shù)組被序列化。這是為什么?因?yàn)樾蛄谢疉rrayList的時候,ArrayList里面的elementData未必是滿的,比方說elementData有10的大小,但是我只用了其中的3個,那么是否有必要序列化整個elementData呢?顯然沒有這個必要,因此ArrayList中重寫了writeObject方法:
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
// Write out element count, and any hidden stuff
int expectedModCount = modCount;
s.defaultWriteObject();
// Write out array length
s.writeInt(elementData.length);
// Write out all elements in the proper order.
for (int i=0; i每次序列化的時候調(diào)用這個方法,先調(diào)用defaultWriteObject()方法序列化ArrayList中的非transient元素,elementData不去序列化它,然后遍歷elementData,只序列化那些有的元素,這樣:
1、加快了序列化的速度
2、減小了序列化之后的文件大小
六、自己編寫一個MyArrayList
package study.collection;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
public class MyArrayList implements List {
//底層用一個數(shù)組接收
private Object[] elementData;
//用于記錄集合的元素個數(shù)
private int size;
/**
* 無參數(shù)構(gòu)造,默認(rèn)為大小10
*/
public MyArrayList()
{
this(10);
}
/**
* 初始化帶容量的構(gòu)造方法
* @param cap
*/
public MyArrayList(int cap)
{
super();
if(cap < 0)
{
throw new IllegalArgumentException("Illegal cap: "+
cap);
}
elementData = new Object[cap];
}
@Override
public boolean add(Object e) {
//1.添加之前確認(rèn)集合中的大小是夠,因此擴(kuò)容判斷
ensureCapacity(size +1); //添加元素一個一個添加,因此size+1;
//2.填充元素
elementData[size++] = e;
return true;
}
/**
* 擴(kuò)容判斷,因?yàn)橹灰砑釉?就需要判斷容器的大小是否滿足
* @param i
*/
private void ensureCapacity(int minCapacity) {
//擴(kuò)容前,需要獲取當(dāng)前的數(shù)組元素的大小
int oldCapacity = elementData.length;
//只有當(dāng)前的容量不滿足,則擴(kuò)容處理
if(oldCapacity < minCapacity)
{
//新大小的比例,采用原來大小的1.5倍
int newCapacity = (oldCapacity * 3)/2 + 1;
//如果新算出來的大小不滿足當(dāng)前需要擴(kuò)容的大小,則就以用戶需要的為主,如果以滿足則以算出來的最佳大小為主
if(newCapacity < minCapacity)
{
newCapacity = minCapacity;
}
//比例算好后,開始執(zhí)行數(shù)組的拷貝操作
Arrays.copyOf(elementData, newCapacity,Object[].class);
}
}
@Override
public void add(int index, Object element) {
//添加指定位置,首先需要做的就是確保索引滿足要求,如果要添加的索引超過了元素個數(shù)中的大小
if(index > size || index < 0)
{
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
}
//如果沒有超過,那么就需要開始添加元素,這個時候同樣需要判斷擴(kuò)容
ensureCapacity(size +1); //添加元素一個一個添加,因此size+1;
//接著需要做的事情是需要將原來位于index 位置的元素,向后面移動
//首先判斷,index的后面是否還有元素
int modNum = size - index;
if(modNum > 0)
{
System.arraycopy(elementData, index, elementData, index+1, size - index);
}
//如果沒有元素或者已經(jīng)拷貝完成,則直接在對應(yīng)的索引處放置元素即可
elementData[index] = element;
//元素個數(shù)加+1
size++;
}
@Override
public boolean addAll(Collection c) {
//添加集合元素,首先需要將集合轉(zhuǎn)換為數(shù)組,計(jì)算出數(shù)組的大小
Object[] a = c.toArray();
//計(jì)算出需要的長度
int numNew = a.length;
//開始擴(kuò)容判斷
ensureCapacity(size +numNew); //添加元素的個數(shù)為numNew
//開始數(shù)組拷貝
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
@Override
public boolean addAll(int index, Collection c) {
//首先索引的正確性
if (index > size || index < 0)
throw new IndexOutOfBoundsException(
"Index: " + index + ", Size: " + size);
//添加的元素集合轉(zhuǎn)換為數(shù)組,計(jì)算要拷貝的長度,準(zhǔn)備擴(kuò)容
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacity(size + numNew); // Increments modCount
//因?yàn)槭侵付ㄎ恢脭U(kuò)容,因此需要判斷下index后面是否有元素
int numMoved = size - index;
//如果大于0,說明要先空出位置來給a數(shù)組
if(numMoved > 0)
{
System.arraycopy(elementData, index, elementData, index+1, size-index);
}
//空出為位置后,然后將集合的元素放到空出的位置上面
System.arraycopy(a, 0, elementData,index, numNew);
size += numNew;
return numNew != 0;
}
@Override
public void clear() {
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
@Override
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
@Override
public boolean containsAll(Collection c) {
//迭代器暫不去實(shí)現(xiàn)
return false;
}
@Override
public Object get(int index) {
//對于數(shù)組而言,根據(jù)索引獲取元素非常簡單,但需要先檢查inde的合法性,避免越界
RangeCheck(index);
return elementData[index];
}
private void RangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(
"Index: "+index+", Size: "+size);
}
@Override
public int indexOf(Object o) {
//循環(huán)遍歷,找出元素,注意是equals比較
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;
}
@Override
public boolean isEmpty() {
return size == 0;
}
@Override
public Iterator iterator() {
//涉及迭代器,暫不去關(guān)注
return null;
}
@Override
public int lastIndexOf(Object o) {
//反向獲取,則反向循環(huán)
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;
}
@Override
public ListIterator listIterator() {
//涉及迭代器,暫不去關(guān)注
return null;
}
@Override
public ListIterator listIterator(int index) {
//涉及迭代器,暫不去關(guān)注
return null;
}
@Override
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
//找到指定的索引開始刪除
private void fastRemove(int index) {
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null;
}
@Override
public Object remove(int index) {
//合法性檢查
RangeCheck(index);
//取出原來老的元素,以便返回
Object oldValue = elementData[index];
//需要開始拷貝數(shù)組,因?yàn)閯h除了索引處的元素,那么則需要向前移動元素
//需要看后面有沒有移動的元素,-1 是減去當(dāng)前這個刪除的元素
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index,
numMoved);//從index+1 開始拷貝到 index 處
elementData[--size] = null; //元素個數(shù)減去一,同事最后一個位置置空,等待垃圾回收
return oldValue;
}
@Override
public boolean removeAll(Collection c) {
////涉及迭代器,暫不去關(guān)注
return false;
}
@Override
public boolean retainAll(Collection c) {
////涉及迭代器,暫不去關(guān)注
return false;
}
@Override
public Object set(int index, Object element) {
RangeCheck(index);
Object oldValue = elementData[index];
elementData[index] = element;
return oldValue;
}
@Override
public int size() {
return size;
}
@Override
public List subList(int fromIndex, int toIndex) {
////涉及迭代器,暫不去關(guān)注
return null;
}
@Override
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
@Override
public Object[] toArray(Object[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
//測試
public static void main(String[] args)
{
MyArrayList list = new MyArrayList();
list.add("333");
list.add("444");
list.add("5");
list.add("344433");
list.add("333");
list.add("333");
System.out.println(list.size());
// System.out.println(list.get(6));
list.remove("444");
System.out.println(list.size());
}
}
說明:其他關(guān)于JDK1.6 和 1.7 和 1.8 的區(qū)別可以看:
JDK1.8、JDK1.7、JDK1.6區(qū)別看這里
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。