前言:看 ArrayList 的源碼,發現 remove 方法主要依賴了 System.arraycopy() 方法實現的。因此須要瞭解一下這個方法如何使用。轉載請註明出處:https://www.cnblogs.com/yuxiaole/p/9951819.htmlhtml
帶註釋的源碼:數組
/** * Copies an array from the specified source array, beginning at the * specified position, to the specified position of the destination array. * A subsequence of array components are copied from the source * array referenced by <code>src</code> to the destination array * referenced by <code>dest</code>. The number of components copied is * equal to the <code>length</code> argument. The components at * positions <code>srcPos</code> through * <code>srcPos+length-1</code> in the source array are copied into * positions <code>destPos</code> through * <code>destPos+length-1</code>, respectively, of the destination * array. * <p> * If the <code>src</code> and <code>dest</code> arguments refer to the * same array object, then the copying is performed as if the * components at positions <code>srcPos</code> through * <code>srcPos+length-1</code> were first copied to a temporary * array with <code>length</code> components and then the contents of * the temporary array were copied into positions * <code>destPos</code> through <code>destPos+length-1</code> of the * destination array. * <p> * If <code>dest</code> is <code>null</code>, then a * <code>NullPointerException</code> is thrown. * <p> * If <code>src</code> is <code>null</code>, then a * <code>NullPointerException</code> is thrown and the destination * array is not modified. * <p> * Otherwise, if any of the following is true, an * <code>ArrayStoreException</code> is thrown and the destination is * not modified: * <ul> * <li>The <code>src</code> argument refers to an object that is not an * array. * <li>The <code>dest</code> argument refers to an object that is not an * array. * <li>The <code>src</code> argument and <code>dest</code> argument refer * to arrays whose component types are different primitive types. * <li>The <code>src</code> argument refers to an array with a primitive * component type and the <code>dest</code> argument refers to an array * with a reference component type. * <li>The <code>src</code> argument refers to an array with a reference * component type and the <code>dest</code> argument refers to an array * with a primitive component type. * <
不帶註釋的源碼:ide
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
src: 源數組; 測試
srcPos: 源數組要複製的起始位置;spa
dest: 目的數組; 指針
destPos: 目的數組放置的起始位置; code
length: 複製的長度。component
一、拷貝過程:將 src 數組的下標爲 srcPos 到 srcPos + length - 1 拷貝到 dest 數組下標爲 destPos 到 destPos + length- 1 的位置。orm
二、這個也支持 src 和 dest 爲同一個數組,這時會將原數組拷貝到臨時數組裏,而後再將臨時數組的數據拷貝到目標數組。htm
三、若是 src 或者 dest 爲 null ,則會拋空指針異常。
四、若是 src 或者 dest 不是數組,則會拋出 ArrayStoreException。
五、src,dest 不是同類型或者能夠進行轉換的數組,則會拋出 ArrayStoreException。
六、若是 srcPos + length > length或者 destPos + lenght > length,則會拋出 IndexOutOfBoundsException。
轉載請註明出處:https://www.cnblogs.com/yuxiaole/p/9951819.html