Arrays是数组的工具类,其提供了许多的工具类方法,下面将分别对其一一介绍。

(一)copyOf方法

Arrays当中的copyOf方法主要分为两种类型,一种是对primitive类型数组的复制,一种是对引用类型数组的复制,其最终都是调用System.arraycopy方法。分别将各种类型写成不同的方法,这样做一是充分利用JAVA面向对象多态的思路,二是避免因类型不匹配时出现异常,例子如下:

 基本数据类型数组的复制,首先new出一个指定长度的数组,然后调用System.arraycopy方法进行复制。

    /**
     * Copies the specified array, truncating or padding with zeros (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>0</tt>.
     * Such indices will exist if and only if the specified length
     * is greater than that of the original array.
     *
     * @param original the array to be copied
     * @param newLength the length of the copy to be returned
     * @return a copy of the original array, truncated or padded with zeros
     *     to obtain the specified length
     * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
     * @throws NullPointerException if <tt>original</tt> is null
     * @since 1.6
     */
    public static int[] copyOf(int[] original, int newLength) {
        int[] copy = new int[newLength];
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    }
copyOf(int[] original, int newLength)

引用类型数组的复制,首先判断目标对象是不是数组类型,如果是数组类型,则直接创建一个对象数组,并将其转换成泛型数组,(T[]) new Object[newLength];如果不是数组类型则根据传入的目标类型,创建出一个指定长度的数组实例出来,(T[]) Array.newInstance(newType.getComponentType(), newLength),最后还是调用System.arraycopy方法进行复制。

   public static <T> T[] copyOf(T[] original, int newLength) {
        return (T[]) copyOf(original, newLength, original.getClass());
    }

    /**
     * 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;
    }
copyOf(T[] original, int newLength)

System.arraycopy方法是一个native方法,它有5个参数,分别为被复制的数组src,被复制数组起始元素位置srcPos,复制到的数组dest,元素复制到数组的起始位置destPos,需要复制元素的个数length。在这里要特别注意,越界异常和空指针异常,当srcPos<0或者destPos<0或者srcPos+length>src.length或者destPos+length>dest.length时出现IndexOutOfBoundsException。src或srcPos为空时出现空指针异常。其他情况则为ArrayStoreException。

    /*
     * @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);
arraycopy(Object src, int srcPos, Object dest, int destPos,int length);

 

posted on 2016-11-19 04:07  天狼群星  阅读(101)  评论(0)    收藏  举报