目录

static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

描述 (Description)

java.lang.System.arraycopy()方法将指定源数组中的数组从指定位置开始复制到目标数组的指定位置。 数组组件的子序列从src引用的源数组复制到dest引用的目标数组。复制的组件数等于length参数。

源数组中位置destPosdestPos + length - 1分别被复制到目标数组的destPosdestPos + length - 1位置。

声明 (Declaration)

以下是java.lang.System.arraycopy()方法的声明

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

参数 (Parameters)

  • src - 这是源数组。

  • srcPos - 这是源数组中的起始位置。

  • dest - 这是目标数组。

  • destPos - 这是目标数据中的起始位置。

  • length - 这是要复制的数组元素的数量。

返回值 (Return Value)

此方法不返回任何值。

异常 (Exception)

  • IndexOutOfBoundsException - 如果复制将导致访问数组边界外的数据。

  • ArrayStoreException - 如果由于类型不匹配而导致src数组中的元素无法存储到dest数组中。

  • NullPointerException - 如果src或dest为null。

例子 (Example)

以下示例显示了java.lang.System.arraycopy()方法的用法。

package com.iowiki;
import java.lang.*;
public class SystemDemo {
   public static void main(String[] args) {
      int arr1[] = { 0, 1, 2, 3, 4, 5 };
      int arr2[] = { 5, 10, 20, 30, 40, 50 };
      // copies an array from the specified source array
      System.arraycopy(arr1, 0, arr2, 0, 1);
      System.out.print("array2 = ");
      System.out.print(arr2[0] + " ");
      System.out.print(arr2[1] + " ");
      System.out.print(arr2[2] + " ");
      System.out.print(arr2[3] + " ");
      System.out.print(arr2[4] + " ");
   }
}

让我们编译并运行上面的程序,这将产生以下结果 -

array2 = 0 10 20 30 40 
↑回到顶部↑
WIKI教程 @2018