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 元素