System.arraycopy () using

Source: Internet
Author: User
1. int elements[] = {1, 2, 3, 4, 5, 6};
New array after enlargement
int hold[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
Copy all the elements in the elements array to the
Hold array, subscript starting from 0
System.arraycopy (elements, 0, hold, 0, elements.length);
for (int i=0;i<elements.length;i++)
System.out.println ("elements[" +i+ "]=" +elements[i));
for (int i=0;iSystem.out.println ("hold[" +i+ "]=" +hold[i));

Last night the Wind explained:

Arraycopy is a method of replicating between arrays. The parameters include two parts:

The first parameter is the copied array, the second argument is the starting position, 0 is the first element, the third is the destination array, and the fifth is the starting position and length of the destination array respectively.

2.
public static void Arraycopy (object src,
                             int srcpos,
                             object dest,
                             int destpos,
                             int length)
Copies an array from the specified source array, starting at the specified location and ending at the specified position in the destination array. From the source array referenced by SRC to the target array referenced by dest, a subsequence of the array component is replicated. The number of the copied component equals the length parameter. The components in the source array that are located between Srcpos and Srcpos+length-1 are copied to the Destpos to destpos+length-1 position in the target array, respectively.

If the parameter src and dest refer to the same array object, the copy execution occurs as if the component Srcpos to the srcpos+length-1 position is first copied to a temporary array with a length component, and then the contents of this temporary array are copied to the target array destpo s to destpos+length-1 position.

If dest is null, the NullPointerException exception is thrown.

If SRC is null, the NullPointerException exception is thrown, and the destination array is not modified.

Otherwise, if any of the following are true, the arraystoreexception exception is thrown and the destination array is not modified: the SRC parameter refers to a non-array object. The dest parameter refers to a non-array object. The SRC and Dest parameters refer to those arrays whose component types are of different base types. The SRC parameter refers to an array with the base component type and the Dest parameter refers to an array with a reference component type. The SRC parameter refers to an array that has a reference component type and the Dest parameter refers to an array with the base component type.

Otherwise, the indexoutofboundsexception exception is thrown as long as any of the following are true, and the target array is not modified: The Srcpos parameter is negative. The Destpos parameter is negative. The length parameter is negative. Srcpos+length is greater than src.length, the length of the source array. Destpos+length is greater than dest.length, that is, the length of the target array.

Otherwise, a Arraystoreexception exception is thrown if the actual component Srcpos to the srcpos+length-1 position in the source array is not converted to a component type of the destination array by assigning the transformation. In this case, the K is set to a minimum non-negative integer smaller than the length, so that src[srcpos+k is not converted to the component type of the destination array, and when an exception is thrown, the source array component from Srcpos to srcpos+k-1 position is copied to the Destpos in the destination array. Destpos+k-1 location, and other locations in the target array are not modified. (Because of the limitations that have been elaborated, this paragraph can only be applied effectively to situations where two arrays have component types that reference types.) )

Parameter: The src -source array. Srcpos -the starting position in the source array. dest -the target array. Destpos -the starting position in the target data. length -the number of array elements to copy. Thrown: indexoutofboundsexception -If replication causes access to data outside of the array scope. arraystoreexception -if the elements in the   src  array cannot be stored in the   dest  array because of a type mismatch. nullpointerexception -if   src  or   dest  is   null.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.