ArrayList是List接口的可變數組的實現。實現了全部可選列表操做,並容許包括 null 在內的全部元素。除了實現 List 接口外,此類還提供一些方法來操做內部用來存儲列表的數組的大小。java
每一個ArrayList實例都有一個容量,該容量是指用來存儲列表元素的數組的大小。它老是至少等於列表的大小(若是不指定capacity,默認是10)。c++
/** * Constructs an empty list with an initial capacity of ten. */ public ArrayList() { this(10); }
隨着向ArrayList中不斷添加元素,其容量也自動增加。自動增加會帶來數據向新數組的從新拷貝(影響性能),所以,若是可預知數據量的多少,可在構造ArrayList時指定其容量。在添加大量元素前,應用程序也能夠使用ensureCapacity操做來增長ArrayList實例的容量,這能夠減小遞增式再分配的數量。 數據庫
/** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return <tt>true</tt> (as specified by {@link Collection#add}) */ public boolean add(E e) { ensureCapacity(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
/** * Increases the capacity of this <tt>ArrayList</tt> instance, if * necessary, to ensure that it can hold at least the number of elements * specified by the minimum capacity argument. * * @param minCapacity the desired minimum capacity */ public void ensureCapacity(int minCapacity) { modCount++; int oldCapacity = elementData.length; if (minCapacity > oldCapacity) { Object oldData[] = elementData; int newCapacity = (oldCapacity * 3)/2 + 1; if (newCapacity < minCapacity) newCapacity = minCapacity; // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); } }
注意: 在ensureCapacity中,數組
Object oldData[] = elementData;
是要在複製新數組前,保存原來數組的引用,由於後面這個引用會指向新的數組。可是保存後其實沒有用處。安全
elementData = Arrays.copyOf(elementData, newCapacity);
注意,此實現不是同步的。若是多個線程同時訪問一個ArrayList實例,而其中至少一個線程從結構上修改了列表,那麼它必須保持外部同步。
網絡
private transient Object[] elementData;
public ArrayList() { this(10); } public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; } public ArrayList(Collection<? extends E> c) { elementData = c.toArray(); size = elementData.length; // c.toArray might (incorrectly) not return Object[] (see 6260652) if (elementData.getClass() != Object[].class) elementData = Arrays.copyOf(elementData, size, Object[].class); }
此處也是用Arrays.copyOf實現數組的複製,重點研究一下。與生成新數組的時候同樣。app
第一判斷ensureSize,若是夠直接插入,不然按照policy擴展,複製,重建數組。dom
第二步插入元素。ide
ArrayList提供了set(int index, E element)、add(E e)、add(int index, E element)、addAll(Collection<? extends E> c)、addAll(int index, Collection<? extends E> c)這些添加元素的方法。下面咱們一一講解性能
1. set(int index, E element),取代,而非插入,返回被取代的元素
/** * Replaces the element at the specified position in this list with * the specified element. * * @param index index of the element to replace * @param element element to be stored at the specified position * @return the element previously at the specified position * @throws IndexOutOfBoundsException {@inheritDoc} */ public E set(int index, E element) { RangeCheck(index); E oldValue = (E) elementData[index]; elementData[index] = element; return oldValue; }
2. add(E e) 增長元素到末尾,若是size不溢出,自動增加
/** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return <tt>true</tt> (as specified by {@link Collection#add}) */ public boolean add(E e) { ensureCapacity(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
3. add(int index, E element) 增長元素到某個位置,該索引以後的元素都後移一位
/** * Inserts the specified element at the specified position in this * list. Shifts the element currently at that position (if any) and * any subsequent elements to the right (adds one to their indices). * * @param index index at which the specified element is to be inserted * @param element element to be inserted * @throws IndexOutOfBoundsException {@inheritDoc} */ public void add(int index, E element) { if (index > size || index < 0) throw new IndexOutOfBoundsException( "Index: "+index+", Size: "+size); ensureCapacity(size+1); // Increments modCount!! System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; }
3.後面兩個方法都是把集合轉換爲數組利用c.toArray,而後利用Arrays.copyOF 方法,重點研究一下
/** * Appends all of the elements in the specified collection to the end of * this list, in the order that they are returned by the * specified collection's Iterator. The behavior of this operation is * undefined if the specified collection is modified while the operation * is in progress. (This implies that the behavior of this call is * undefined if the specified collection is this list, and this * list is nonempty.) * * @param c collection containing elements to be added to this list * @return <tt>true</tt> if this list changed as a result of the call * @throws NullPointerException if the specified collection is null */ public boolean addAll(Collection<? extends E> c) { Object[] a = c.toArray(); int numNew = a.length; ensureCapacity(size + numNew); // Increments modCount System.arraycopy(a, 0, elementData, size, numNew); size += numNew; return numNew != 0; }
Collection 接口定義了toArray方法,以下
/** * Returns an array containing all of the elements in this collection. * If this collection makes any guarantees as to what order its elements * are returned by its iterator, this method must return the elements in * the same order. * * <p>The returned array will be "safe" in that no references to it are * maintained by this collection. (In other words, this method must * allocate a new array even if this collection is backed by an array). * The caller is thus free to modify the returned array. * * <p>This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this collection */ Object[] toArray();
下面是arraylist對其的一種實現:
/** * Returns an array containing all of the elements in this list * in proper sequence (from first to last element). * * <p>The returned array will be "safe" in that no references to it are * maintained by this list. (In other words, this method must allocate * a new array). The caller is thus free to modify the returned array. * * <p>This method acts as bridge between array-based and collection-based * APIs. * * @return an array containing all of the elements in this list in * proper sequence */ public Object[] toArray() { return Arrays.copyOf(elementData, size); }
備註:
/**
* This class contains various methods for manipulating arrays (such as
* sorting and searching). This class also contains a static factory
* that allows arrays to be viewed as lists.
一種是按索引刪除,不用查詢,索引以後的element順序左移一位,並將最後一個element設爲null,由gc負責回收。
/** * Removes the element at the specified position in this list. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param index the index of the element to be removed * @return the element that was removed from the list * @throws IndexOutOfBoundsException {@inheritDoc} */ public E remove(int index) { RangeCheck(index); modCount++; E oldValue = (E) elementData[index]; int numMoved = size - index - 1; if (numMoved > 0) System.arraycopy(elementData, index+1, elementData, index, numMoved); elementData[--size] = null; // Let gc do its work return oldValue; }
源碼以下:
/** * Copies the specified array, truncating or padding with nulls (if necessary) * so the copy has the specified length. For all indices that are * valid in both the original array and the copy, the two arrays will * contain identical values. For any indices that are valid in the * copy but not the original, the copy will contain <tt>null</tt>. * Such indices will exist if and only if the specified length * is greater than that of the original array. * The resulting array is of the class <tt>newType</tt>. * * @param original the array to be copied * @param newLength the length of the copy to be returned * @param newType the class of the copy to be returned * @return a copy of the original array, truncated or padded with nulls * to obtain the specified length * @throws NegativeArraySizeException if <tt>newLength</tt> is negative * @throws NullPointerException if <tt>original</tt> is null * @throws ArrayStoreException if an element copied from * <tt>original</tt> is not of a runtime type that can be stored in * an array of class <tt>newType</tt> * @since 1.6 */ public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) { T[] copy = ((Object)newType == (Object)Object[].class) ? (T[]) new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(), newLength); System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength)); return copy; }
此處jdk有所優化,若是是object類型,直接new object數組,若是不是經過Array.newInstance調用native方法產生響應的數組類型,建立數組後,經過System.arrayCopy實現數組複製,System是個final類,copyof是個native方法。(若是實現,爲什麼用native方法???)源碼以下
* @param src the source array. * @param srcPos starting position in the source array. * @param dest the destination array. * @param destPos starting position in the destination data. * @param length the number of array elements to be copied. * @exception IndexOutOfBoundsException if copying would cause * access of data outside array bounds. * @exception ArrayStoreException if an element in the <code>src</code> * array could not be stored into the <code>dest</code> array * because of a type mismatch. * @exception NullPointerException if either <code>src</code> or * <code>dest</code> is <code>null</code>. */ public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
備註:
Native修飾符標示該方法的實現體非java,而是c++或者其餘語言
2. 有助於提高性能
Java不是完美的,Java的不足除了體如今運行速度上要比傳統的C++慢許多以外,Java沒法直接訪問到操做系統底層(如系統硬件等),爲此Java使用native方法來擴展Java程序的功能。
能夠將native方法比做Java程序同C程序的接口,其實現步驟:
1、在Java中聲明native()方法,而後編譯;
2、用javah產生一個.h文件;
3、寫一個.cpp文件實現native導出方法,其中須要包含第二步產生的.h文件(注意其中又包含了JDK帶的jni.h文件);
4、將第三步的.cpp文件編譯成動態連接庫文件;
5、在Java中用System.loadLibrary()方法加載第四步產生的動態連接庫文件,這個native()方法就能夠在Java中被訪問了。
上述例子中,System.arrayCopy()和Array.newInsance(compnentType, length)均採用native方法,補充Array.newInstance()源碼以下:
* @param componentType the <code>Class</code> object representing the * component type of the new array * @param length the length of the new array * @return the new array * @exception NullPointerException if the specified * <code>componentType</code> parameter is null * @exception IllegalArgumentException if componentType is {@link Void#TYPE} * @exception NegativeArraySizeException if the specified <code>length</code> * is negative */ public static Object newInstance(Class<?> componentType, int length) throws NegativeArraySizeException { return newArray(componentType, length); }
private static native Object newArray(Class componentType, int length) throws NegativeArraySizeException;
利用Array.newInstance()建立數組的意義是什麼?
Java反射技術除了能夠在運行時動態地決定要建立什麼類型的對象,訪問哪些成員變量,方法,還能夠動態地建立各類不一樣類型,不一樣維度的數組。
動態建立數組的步驟以下:
1.建立Class對象,經過forName(String)方法指定數組元素的類型
2.調用Array.newInstance(Class, length_of_array)動態建立數組
訪問動態數組元素的方法和一般有所不一樣,它的格式以下所示,注意該方法返回的是一個Object對象
Array.get(arrayObject, index)
爲動態數組元素賦值的方法也和一般的不一樣,它的格式以下所示, 注意最後的一個參數必須是Object類型
Array.set(arrayObject, index, object)
動態數組Array不單能夠建立一維數組,還能夠建立多維數組。步驟以下:
1.定義一個整形數組:例如int[] dims= new int{5, 10, 15};指定一個三維數組
2.調用Array.newInstance(Class, dims);建立指定維數的數組
訪問多維動態數組的方法和訪問一維數組的方式沒有什麼大的不一樣,只不過要分屢次來獲取,每次取出的都是一個Object,直至最後一次,賦值也同樣。
動態數組Array能夠轉化爲普通的數組,例如:
Array arry = Array.newInstance(Integer.TYPE,5);
int arrayCast[] = (int[])array;
public static void main(String args[]) throws Exception { Class<?> classType = Class.forName("java.lang.String"); // 建立一個長度爲10的字符串數組 Object array = Array.newInstance(classType, 10); // 把索引位置爲5的元素設爲"hello" Array.set(array, 5, "hello"); // 得到索引位置爲5的元素的值 String s = (String) Array.get(array, 5); System.out.println(s); }
public static void main(String args[]) { int[] dims = new int[] { 5, 10, 15 }; // 建立一個具備指定的組件類型和維度的新數組。 Object array = Array.newInstance(Integer.TYPE, dims); // 取出三維數組的第3行,爲一個數組 Object arrayObj = Array.get(array, 3); Class<?> cls = arrayObj.getClass().getComponentType(); System.out.println(cls); // 取出第3行的第5列,爲一個數組 arrayObj = Array.get(arrayObj, 5); // 訪問第3行第5列的第10個元素,爲其賦值37 Array.setInt(arrayObj, 10, 37); // 動態數組和普通數組的轉換:強行轉換成對等的數組 int arrayCast[][][] = (int[][][]) array; System.out.println(arrayCast[3][5][10]); }
public class ArrayList<E> extends AbstractList<E> implements List<E>, 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;
序列化有2種方式:
A、只是實現了Serializable接口。
序列化時,調用java.io.ObjectOutputStream的defaultWriteObject方法,將對象序列化。
注意:此時transient修飾的字段,不會被序列化。
B、實現了Serializable接口,同時提供了writeObject方法。
序列化時,會調用該類的writeObject方法。而不是java.io.ObjectOutputStream的defaultWriteObject方法。
注意:此時transient修飾的字段,是否會被序列化,取決於writeObject
/** * Save the state of the <tt>ArrayList</tt> instance to a stream (that * is, serialize it). * * @serialData The length of the array backing the <tt>ArrayList</tt> * instance is emitted (int), followed by all of its elements * (each an <tt>Object</tt>) in the proper order. */ 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<size; i++) s.writeObject(elementData[i]); if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } }
ArrayList是會開闢多餘空間來保存數據的,而系列化和反序列化這些沒有存放數據的空間是要消耗更多資源的,因此ArrayList的數組就聲明爲transient,本身實現write/readObject方法,僅僅系列化已經存放的數據
ArrayList 實現了java.io.Serializable接口,在須要序列化的狀況下,複寫writeObjcet和readObject方法提供適合本身的序列化方法。
一、序列化是幹什麼的?
簡單說就是爲了保存在內存中的各類對象的狀態(也就是實例變量,不是方法),而且能夠把保存的對象狀態再讀出來。雖然你能夠用你本身的各類各樣的方法來保存object states,可是Java給你提供一種應該比你本身好的保存對象狀態的機制,那就是序列化。
二、什麼狀況下須要序列化
a)當你想把的內存中的對象狀態保存到一個文件中或者數據庫中時候;
b)當你想用套接字在網絡上傳送對象的時候;
c)當你想經過RMI傳輸對象的時候;
3. 示例代碼
1.import java.io.*;
3.public class Box implements Serializable //要保存的對象類必須實現序列化接口serializable
4.{
5. private int width;
6. private int height;
7.
8. public void setWidth(int width){
9. this.width = width;
10. }
11. public void setHeight(int height){
12. this.height = height;
13. }
14.
15. public static void main(String[] args){
16. Box myBox = new Box();
17. myBox.setWidth(50);
18. myBox.setHeight(30);
19.
20. try{ //序列化。
21. FileOutputStream fs = new FileOutputStream("foo.ser");
22. ObjectOutputStream os = new ObjectOutputStream(fs);
23. os.writeObject(myBox);
24. os.close();
25. }catch(Exception ex){
26. ex.printStackTrace();
27. }
28. }
30.}
發序列化方法
Public static void seserialize(string filename) throws Exception
{
// 反序列化(讀出保存的對象文件)
ObjectInputStream in = new ObjectInputStream (new FileInputStream(filename));
Box box = (Box) (in.readbject());
System.out.println(box.toString());
In.Closer();
}
1.Arraylist基於數組實現,是自增加的
2,非線程安全的
3.插入時可能要擴容,刪除時size不會減小,若是須要,可使用trimToSize方法,在查詢時,遍歷查詢,爲null,判斷是不是null, 返回; 若是不是null,用equals判斷,返回
/** * Returns <tt>true</tt> if this list contains the specified element. * More formally, returns <tt>true</tt> if and only if this list contains * at least one element <tt>e</tt> such that * <tt>(o==null ? e==null : o.equals(e))</tt>. * * @param o element whose presence in this list is to be tested * @return <tt>true</tt> 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 <tt>i</tt> such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, * 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; }
4. 容許重複和 null 元素