欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

System.arraycopy() 和 Arrays.copyOf()方法

程序员文章站 2022-03-10 20:41:02
...

System.arraycopy() 和 Arrays.copyOf()方法都能执行对数组的复制, JDK中很多地方都用到这两个方法, 下面测试一下

 

一  System.arraycopy() 方法

源码:
* @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);

src: 原数组  srcPos: 原数组的起始位置  dest: 目标数组  destPos: 目标数据的起始位置  length: 要复制的数组元素的数量

测试方法:


        int[] a = new int[10];
        a[0] = 0;
        a[1] = 1;
        a[2] = 2;
        a[3] = 3;
        System.arraycopy(a, 2, a, 3, 3);
        for (int i : a) {
            System.out.print("新版" + i + "  ");
        }

        System.out.println();
        System.out.println("牛逼的分隔符" + "-------------------------------------------------------------");


        a[2] = 99;
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + "  ");
        }

输出结果:


新版0  新版1  新版2  新版2  新版3  新版0  新版0  新版0  新版0  新版0  
牛逼的分隔符-------------------------------------------------------------
0  1  99  2  3  0  0  0  0  0  

结论: 

在此数组中的指定位置插入指定的元素。从srcPos开始之后的所有成员后移一个位置;将a[2] = 99 插入srcPos位置; 

二   Arrays.copyOf()方法

源码: 

  * @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;
    }

original: 需要复制的数组      newLength: 新数组的长度

测试: 

 int[] a = new int[3];
        a[0] = 0;
        a[1] = 1;
        a[2] = 2;
        int[] b = Arrays.copyOf(a, 10);
        System.out.println("b.length: " + b.length);
        for (int i : b) {
            System.out.print(i + "  ");
        }

结果: 

b.length: 10
0  1  2  0  0  0  0  0  0  0 

结论: Arrays.copyOf()方法的作用就是给原数组扩容或创建一个扩容后保留原数组内容的新数组!

相关标签: array