ArrayList源码解读比较简单的集合框架

92 阅读4分钟

重要的源码

我认为本身相对比较容易理解 就列举为认为,要了解ArrayList 较重要的方法 该方法使用了9

QQ截图20220513000737.png

/**

  • @param src 源数组。
  • @param srcPos 源数组中的起始位置。
  • @param dest 目标数组。
  • @param destPos 目标数据中的起始位置。
  • @param length 要复制的数组元素的数量
  • @exception IndexOutOfBoundsException if copying would cause */
public static native void arraycopy(Object src,  int  srcPos,
                                    Object dest, int destPos,
                                    int length);

我认为大概的意思就是 src 的第 srcPro 个 复制到 dest 的第 destPost 个 lenght 要复制的数组元素的数量

更具体看下面的源码

/**
 * 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.
 * </ul>
 * <p>
 * Otherwise, if any of the following is true, an
 * <code>IndexOutOfBoundsException</code> is
 * thrown and the destination is not modified:
 * <ul>
 * <li>The <code>srcPos</code> argument is negative.
 * <li>The <code>destPos</code> argument is negative.
 * <li>The <code>length</code> argument is negative.
 * <li><code>srcPos+length</code> is greater than
 *     <code>src.length</code>, the length of the source array.
 * <li><code>destPos+length</code> is greater than
 *     <code>dest.length</code>, the length of the destination array.
 * </ul>
 * <p>
 * Otherwise, if any actual component of the source array from
 * position <code>srcPos</code> through
 * <code>srcPos+length-1</code> cannot be converted to the component
 * type of the destination array by assignment conversion, an
 * <code>ArrayStoreException</code> is thrown. In this case, let
 * <b><i>k</i></b> be the smallest nonnegative integer less than
 * length such that <code>src[srcPos+</code><i>k</i><code>]</code>
 * cannot be converted to the component type of the destination
 * array; when the exception is thrown, source array components from
 * positions <code>srcPos</code> through
 * <code>srcPos+</code><i>k</i><code>-1</code>
 * will already have been copied to destination array positions
 * <code>destPos</code> through
 * <code>destPos+</code><i>k</I><code>-1</code> and no other
 * positions of the destination array will have been modified.
 * (Because of the restrictions already itemized, this
 * paragraph effectively applies only to the situation where both
 * arrays have component types that are reference types.)
 *
 * @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);

源码我也看不懂,尴尬!

System (Java Platform SE 8 )

public static void arraycopy(Object src,
                             int srcPos,
                             Object dest,
                             int destPos,
                             int length)

将指定源数组中的数组从指定位置复制到目标数组的指定位置。 阵列组件的一个子序列被从通过引用的源阵列复制src被引用的目标阵列dest 。 复制的组件数量等于length参数。 源阵列中位置srcPossrcPos+length-1的组件分别复制到目标阵列的位置destPosdestPos+length-1

如果srcdest参数指代相同的数组对象,则被处理是否在位置上的部件进行复印srcPos通过srcPos+length-1首先被复制到一个临时的阵列length组分,然后将临时数组的内容被复制到的位置destPos通过destPos+length-1目标数组的。

如果destnull ,那么会抛出一个NullPointerException

如果srcnull ,则抛出一个NullPointerException并且目标数组不被修改。

否则,如果以下任一为真,一个ArrayStoreException抛出和不修改目标:

  • src参数是指不是数组的对象。
  • dest参数是指不是数组的对象。
  • src参数和dest参数是指组件类型是不同的基本类型的数组。
  • src参数是指具有原始组件类型的数组, dest参数是指具有引用组件类型的数组。
  • src参数是指具有引用组件类型的数组, dest参数是指具有原始组件类型的数组。

否则,如果以下任一为真,一个IndexOutOfBoundsException抛出和不修改目标:

  • srcPos论点是否定的。
  • destPos论点是否定的。
  • length论点是否定的。
  • srcPos+length大于src.length ,源数组的长度。
  • destPos+length大于dest.length ,目标数组的长度。

否则,如果来自位置srcPossrcPos+length-1的源数组的任何实际组件无法通过转换转换为目标数组的组件类型,则抛出ArrayStoreException 。 在这种情况下,令k为比长度小于最小的非负整数,使得src[srcPos+ ķ ]不能转换到组件类型目标数组的; 当抛出异常时,位置srcPossrcPos+ k -1源阵列组件将已经被复制到目标阵列位置destPosdestPos+ k -1 ,并且目标阵列的其他位置将不会被修改。 (由于限制已经列出,本段仅适用于两个数组都具有引用类型的组件类型的情况。)