Home » Java programming language

Java System class arraycopy() method with example

System class arraycopy() method: Here, we are going to learn about the arraycopy() method of System class with its syntax and example.
Submitted by Preeti Jain, on September 14, 2019

System class arraycopy() method

  • arraycopy() method is available in java.lang package.
  • 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() 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() method is static so this method is accessible with the class name too.
  • 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.
    • ArrayStoreException: When source array element could not be copied in destination array due to different casting of an array.
    • NullPointerException: When any one of the given arrays is 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_start_pos – represents the starting or initial position in the source array.
  • dest_array – represents the destination array.
  • dest_start_pos – represents the starting or initial position in the destination array.
  • len – represents the number of elements to be copied.

Return value:

The return type of this method is void, it does not return any value.

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 






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.