java.lang.System.arraycopy()方法使用说明

java.lang.System.arraycopy()方法使用说明

java.lang.System为标准的输入输出,加载文件和类库,访问外部定于属性提供了一些十分有用的方法。 java.lang.System.arraycopy()方法是从一个源数组的指定开始位置拷贝元素到目标数组提到的位置。被拷贝的参数的数目由参数len决定。

source_Positionsource_Position + length – 1的元素拷贝到目标数组的destination_Positiondestination_Position + length – 1的位置处。

语法说明:
public static void arraycopy(Object source_arr, int sourcePos, Object dest_arr, int destPos, int len)
参数: 
source_arr : 源数组
    sourcePos : 源数组拷贝元素的起始位置
dest_arr : 目标数组
destPos : 目标数组接收拷贝元素的起始位置
len : 拷贝的元素的数目


示例代码:
public static void main(String[] args) {
int arry1[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
int arry2[] = { 15, 25, 35, 45, 55, 65, 75, 85, 95, 105 };
int source_arr[], sourcePos, dest_arr[], destPos, len;
source_arr = arry1;
sourcePos = 3;
dest_arr = arry2;
destPos = 5;
len = 4;


System.out.print("source_array : ");
for (int i = 0; i < arry1.length; i++)
System.out.print(arry1[i] + " ");
System.out.println("");
System.out.println("sourcePos : " + sourcePos);
// Print elements of source
System.out.print("dest_array : ");
for (int i = 0; i < arry2.length; i++)
System.out.print(arry2[i] + " ");
System.out.println("");
System.out.println("destPos : " + destPos);
System.out.println("len : " + len);
// Use of arraycopy() method
System.arraycopy(source_arr, sourcePos, dest_arr, destPos, len);
// Print elements of destination after
System.out.print("final dest_array : ");
for (int i = 0; i < arry2.length; i++)
System.out.print(arry2[i] + " ");
}

执行结果:
source_array : 10 20 30 40 50 60 70 80 90 100 
sourcePos : 3
dest_array : 15 25 35 45 55 65 75 85 95 105 
destPos : 5
len : 4
final dest_array : 15 25 35 45 55 40 50 60 70 105 


猜你喜欢

转载自blog.csdn.net/u010786396/article/details/78542912