Friday, February 8, 2013

Array copy method

8:09 AM

Copies a specified portion of a source array, starting from a specified 
position, to a destination array, starting at a specified position. 
 
public static void arraycopy(
    java.lang.Object src,
    int src_position,
    java.lang.Object dst,
    int dst_position,
    int length);

public class MyClass
{
    public static void main(String[] args)
    {
        // Declare two arrays:
        int[] arr1 = {1,2,3,4,5,6};
        int[] arr2 = {0,2,4,6,8,10};
        
        // Copy two elements from arr1 starting from the second element
        // into arr2 starting at the fourth element:
        System.arraycopy(
                        arr1,
                        1,       // Second element.
                        arr2,
                        3,       // Fourth element.
                        2        // Length = 2.
                        );

        // Display the new array (arr2):
        for (int i=0; i<arr2.length; i++)
            System.out.println("Element #" + i + " = " + arr2[i]);
    }
}

/*
Output:
Element #0 = 0
Element #1 = 2
Element #2 = 4
Element #3 = 2
Element #4 = 3
Element #5 = 10
*/

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

 

© 2013 CSC. All rights resevered. Designed by Templateism

Back To Top