作为一个Android开发者,对这两个方法挺陌生的,只有在分析阅读ArrayList的源码才算是真正接触到这两个方法。在此记录下这两个方法的用法及其源码。
将指定源数组中的数组从指定位置复制到目标数组的指定位置。
package zzw.cn.listtest;
/**
* @author 鹭岛猥琐男
* @create 2019/8/7 19:55
*/
public class TestSystemArrayCopy
{
public static void main(String[] args)
{
int[] src = {1, 2, 3, 4, 5};
int[] dest = {6, 7, 8, 9, 10,11,12,13};
System.out.println("源数组src:");
for (int i = 0; i < src.length; i++)
{
if (i != src.length - 1)
{
System.out.print(src[i] + ",");
} else
{
System.out.print(src[i]);
}
}
//调用System.arraycopy
System.arraycopy(src, 0, dest, 2, 3);
System.out.println("\n目标数组arr:");
for (int i = 0; i < dest.length; i++)
{
if (i != dest.length - 1)
{
System.out.print(dest[i] + ",");
} else
{
System.out.print(dest[i]);
}
}
}
}
结果:
源数组src:
1,2,3,4,5
目标数组arr:
6,7,1,2,3,11,12,13
Process finished with exit code 0
/*
* @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 src
* array could not be stored into the dest
array
* because of a type mismatch.
* @exception NullPointerException if either src
or
* dest
is null
.
*/
public static native void arraycopy(Object src, int srcPos,
Object dest, int destPos,
int length);
此方法是一个native方法,无法看到具体的实现 。几个参数的含义:
src:源数组;
srcPos:源数组要复制的起始位置;
dest:目的数组;
destPos:目的数组放置的起始位置;
length:复制的长度。
Arrays.copyOf()不仅仅只是拷贝数组中的元素,在拷贝元素时,会创建一个新的数组对象。
package zzw.cn.listtest;
import java.util.Arrays;
/**
* @author 鹭岛猥琐男
* @create 2019/8/7 19:42
*/
public class Test
{
public static void main(String[] args)
{
//原数组
int[] arr = {1, 2, 3, 4, 5};
System.out.println("原数组arr:");
for (int i = 0; i < arr.length; i++)
{
if (i != arr.length - 1)
{
System.out.print(arr[i] + ",");
} else
{
System.out.print(arr[i]);
}
}
//对原数组arr复制,产生一个新的数组
int[] arrNew = Arrays.copyOf(arr, 10);
System.out.println("\n新数组arr:");
for (int i = 0; i < arrNew.length; i++)
{
if (i != arrNew.length - 1)
{
System.out.print(arrNew[i] + ",");
} else
{
System.out.print(arrNew[i]);
}
}
}
}
运行结果:
原数组arr:
1,2,3,4,5
新数组arr:
1,2,3,4,5,0,0,0,0,0
Process finished with exit code 0
/*
* @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 newLength is negative
* @throws NullPointerException if original 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 :要返回副本的长度
从源码可以看出,首先创建一个数组copy ,此数组的长度为要返回副本的长度(newLength),接着调用 System.arraycopy 将要复制数组复制到新创建的数组 copy 中。复制从要复制的数组 original 的第一位开始,复制到目标数组 copy 中。