话不多说,直接上代码,跑一遍一目了然
import java.util.Arrays;
public class ArrayCopyDemo {
public static void main(String[] args) {
testSystemArrayCopy();
}
public static void testSystemArrayCopy() {
int[] src = {1, 2, 3};
int[] dest = new int[5];
/**
* arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* 参数含义:
* src:源数组
* srcPos:源数组起始索引
* dest:目标数组
* destPos:目标数组起始索引
* length:需要拷贝的长度
*/
System.arraycopy(src, 0, dest, 0, src.length);
System.out.println(Arrays.toString(dest)); // [1, 2, 3, 0, 0]
}
}
另外Arrays中的copyOf及copyOfRange方法包装了System.arraycopy,属于简化版本,调用简单,有兴趣的可自行研究。