系统类arraycopy()方法 (System class arraycopy() method)
arraycopy() method is available in java.lang package.
arraycopy()方法在java.lang包中可用。
arraycopy() method is used to copy an array from the given argument (src_array) and copying starting at the given position(src_start_pos), to the given position (dest_start_pos) of the given destination array (dest_array).
arraycopy()方法被用于从给定的参数(src_array)和复制开始在给定位置(src_start_pos)复制的阵列,为给定的位置给定目标阵列(dest_array)的(dest_start_pos)。
arraycopy() method a subsequence of array elements are copied from the source array addressed by src_array to the destination array addressed by dest_array.
数组元素的亚序列从源阵列复制arraycopy()方法来解决由src_array到目的地阵列寻址通过dest_array。
arraycopy() method is static so this method is accessible with the class name too.
arraycopy()方法是静态的,因此也可以使用类名访问此方法。
This method may be thrown various type of exception and the exception are given below:
该方法可能会引发各种类型的异常,下面给出了该异常:
- IndexOutfBoundsException: While copying an element cause access of elements outside array bounds. IndexOutfBoundsException:复制元素时,导致访问数组范围之外的元素。
- ArrayStoreException: When source array element could not be copied in destination array due to different casting of an array. ArrayStoreException:当源数组元素由于数组的不同转换而无法复制到目标数组中时。
- NullPointerException: When any one of the given arrays is null.NullPointerException:当给定数组中的任何一个为null时。
Syntax:
句法:
public static void arraycopy(
Object src_array,
int src_start_pos,
Object dest_array,
int dest_start_pos,
int len);
Parameter(s):
参数:
src_array – represents the source array.
src_array –表示源数组。
src_start_pos – represents the starting or initial position in the source array.
src_start_pos –表示源数组中的开始或初始位置。
dest_array – represents the destination array.
dest_array –表示目标数组。
dest_start_pos – represents the starting or initial position in the destination array.
dest_start_pos –表示目标数组中的开始或初始位置。
len – represents the number of elements to be copied.
len –表示要复制的元素数。
Return value:
返回值:
The return type of this method is void, it does not return any value.
此方法的返回类型为void ,它不返回任何值。
Example:
例:
// Java program to demonstrate the example of
// arraycopy() method of System Class.
public class ArraycopyMethod {
public static void main(String[] args) {
// Here we are declaring source and destination array
int src_array[] = {
10,
20,
30,
40,
50
};
int dest_array[] = {
60,
70,
80,
90,
100,
110,
120,
130,
140,
150,
160
};
// By using arraycopy() method to copy a source
// array to destination array
System.arraycopy(src_array, 3, dest_array, 0, 2);
// Display destination array elements
System.out.println(dest_array[0] + " ");
System.out.println(dest_array[1] + " ");
System.out.println(dest_array[2] + " ");
System.out.println(dest_array[3] + " ");
System.out.println(dest_array[4] + " ");
System.out.println(dest_array[5] + " ");
System.out.println(dest_array[6] + " ");
System.out.println(dest_array[7] + " ");
System.out.println(dest_array[8] + " ");
System.out.println(dest_array[9] + " ");
System.out.println(dest_array[10] + " ");
}
}
Output
输出量
E:\Programs>javac ArraycopyMethod.java
E:\Programs>java ArraycopyMethod
40
50
80
90
100
110
120
130
140
150
160
翻译自: https://www.includehelp.com/java/system-class-arraycopy-method-with-example.aspx