原文地址:java.lang.System.arraycopy() method
java.lang.System为标准的输入输出,加载文件和类库,访问外部定于属性提供了一些十分有用的方法。 java.lang.System.arraycopy()方法是从一个源数组的指定开始位置拷贝元素到目标数组提到的位置。被拷贝的参数的数目由参数len决定。
从source_Position到source_Position + length – 1的元素拷贝到目标数组的destination_Position到destination_Position + length – 1的位置处。
类声明
public final class System extends Object
语法:
public static void arraycopy(Object source_arr, int sourcePos, Object dest_arr, int destPos, int len)
参数:
source_arr : 源数组
sourcePos : 源数组拷贝元素的起始位置
dest_arr : 目标数组
destPos : 目标数组接收拷贝元素的起始位置
len : 拷贝的元素的数目
实现:
// Java program explaining System class method - arraycopy()
import java.lang.*;
public class NewClass
{
public static void main(String[] args)
{
int s[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
int d[] = { 15, 25, 35, 45, 55, 65, 75, 85, 95, 105};
int source_arr[], sourcePos, dest_arr[], destPos, len;
source_arr = s;
sourcePos = 3;
dest_arr = d;
destPos = 5;
len = 4;
// Print elements of source
System.out.print("source_array : ");
for (int i = 0; i < s.length; i++)
System.out.print(s[i] + " ");
System.out.println("");
System.out.println("sourcePos : " + sourcePos);
// Print elements of source
System.out.print("dest_array : ");
for (int i = 0; i < d.length; i++)
System.out.print(d[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 < d.length; i++)
System.out.print(d[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
译者编
下面是JDK对arraycopy的解释:
/**
* Copies an array from the specified source array, beginning at the
* specified position, to the specified position of the destination array.
* A subsequence of array components are copied from the source
* array referenced by <code>src</code> to the destination array
* referenced by <code>dest</code>. The number of components copied is
* equal to the <code>length</code> argument. The components at
* positions <code>srcPos</code> through
* <code>srcPos+length-1</code> in the source array are copied into
* positions <code>destPos</code> through
* <code>destPos+length-1</code>, respectively, of the destination
* array.
* <p>
* If the <code>src</code> and <code>dest</code> arguments refer to the
* same array object, then the copying is performed as if the
* components at positions <code>srcPos</code> through
* <code>srcPos+length-1</code> were first copied to a temporary
* array with <code>length</code> components and then the contents of
* the temporary array were copied into positions
* <code>destPos</code> through <code>destPos+length-1</code> of the
* destination array.
* <p>
* If <code>dest</code> is <code>null</code>, then a
* <code>NullPointerException</code> is thrown.
* <p>
* If <code>src</code> is <code>null</code>, then a
* <code>NullPointerException</code> is thrown and the destination
* array is not modified.
* <p>
* Otherwise, if any of the following is true, an
* <code>ArrayStoreException</code> is thrown and the destination is
* not modified:
* <ul>
* <li>The <code>src</code> argument refers to an object that is not an
* array.
* <li>The <code>dest</code> argument refers to an object that is not an
* array.
* <li>The <code>src</code> argument and <code>dest</code> argument refer
* to arrays whose component types are different primitive types.
* <li>The <code>src</code> argument refers to an array with a primitive
* component type and the <code>dest</code> argument refers to an array
* with a reference component type.
* <li>The <code>src</code> argument refers to an array with a reference
* component type and the <code>dest</code> argument refers to an array
* with a primitive component type.
* </ul>
* <p>
* Otherwise, if any of the following is true, an
* <code>IndexOutOfBoundsException</code> is
* thrown and the destination is not modified:
* <ul>
* <li>The <code>srcPos</code> argument is negative.
* <li>The <code>destPos</code> argument is negative.
* <li>The <code>length</code> argument is negative.
* <li><code>srcPos+length</code> is greater than
* <code>src.length</code>, the length of the source array.
* <li><code>destPos+length</code> is greater than
* <code>dest.length</code>, the length of the destination array.
* </ul>
* <p>
* Otherwise, if any actual component of the source array from
* position <code>srcPos</code> through
* <code>srcPos+length-1</code> cannot be converted to the component
* type of the destination array by assignment conversion, an
* <code>ArrayStoreException</code> is thrown. In this case, let
* <b><i>k</i></b> be the smallest nonnegative integer less than
* length such that <code>src[srcPos+</code><i>k</i><code>]</code>
* cannot be converted to the component type of the destination
* array; when the exception is thrown, source array components from
* positions <code>srcPos</code> through
* <code>srcPos+</code><i>k</i><code>-1</code>
* will already have been copied to destination array positions
* <code>destPos</code> through
* <code>destPos+</code><i>k</I><code>-1</code> and no other
* positions of the destination array will have been modified.
* (Because of the restrictions already itemized, this
* paragraph effectively applies only to the situation where both
* arrays have component types that are reference types.)
*
* @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 <code>src</code>
* array could not be stored into the <code>dest</code> array
* because of a type mismatch.
* @exception NullPointerException if either <code>src</code> or
* <code>dest</code> is <code>null</code>.
**/
public static native void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);