A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/lang/system_arraycopy.htm below:

Java System arraycopy() Method

Java System arraycopy() Method Description

The Java System arraycopy() method 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 src to the destination array referenced by dest.The number of components copied is equal to the length argument.

The components at positions srcPos through srcPos + length - 1 in the source array are copied into positions destPos through destPos + length - 1, respectively, of the destination array.

Declaration

Following is the declaration for java.lang.System.arraycopy() method

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
Parameters Return Value

This method does not return any value.

Exception Example: Copying array of int

The following example shows the usage of Java System arraycopy() method. In this program, we've created two arrays of ints and initialized them with some values. Now using System.arraycopy() method, the first element of the first array arr1 is copied to second array at index 0. Then we've printed the second array to show the updated array as result.

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) {

      int arr1[] = { 0, 1, 2, 3, 4, 5 };
      int arr2[] = { 5, 10, 20, 30, 40, 50 };
    
      // copies an array from the specified source array
      System.arraycopy(arr1, 0, arr2, 0, 1);
      
      System.out.print("array2 = ");
      
      for(int i= 0; i < arr2.length; i++) {
    	  System.out.print(arr2[i] + " ");
      }
   }
}
Output

Let us compile and run the above program, this will produce the following result −

array2 = 0 10 20 30 40 
Example: Copying array of strings

The following example shows the usage of Java System arraycopy() method. In this program, we've created two arrays of strings and initialized them with some values. Now using System.arraycopy() method, the first element of the first array arr1 is copied to second array at index 0. Then we've printed the second array to show the updated array as result.

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) {

      String arr1[] = { "0", "1", "2", "3", "4", "5" };
      String arr2[] = { "5", "10", "20", "30", "40", "50" };
    
      // copies an array from the specified source array
      System.arraycopy(arr1, 0, arr2, 0, 1);
      
      System.out.print("array2 = ");
      
      for(int i= 0; i < arr2.length; i++) {
    	  System.out.print(arr2[i] + " ");
      }
   }
}
Output

Let us compile and run the above program, this will produce the following result −

array2 = 0 10 20 30 40 
Example: Copying array of Objects

The following example shows the usage of Java System arraycopy() method. In this program, we've created two arrays of Student objects and initialized them with some values. Now using System.arraycopy() method, the first element of the first array arr1 is copied to second array at index 0. Then we've printed the second array to show the updated array as result.

package com.tutorialspoint;

public class SystemDemo {

   public static void main(String[] args) {

      Student arr1[] = { new Student(1, "Julie") };
      Student arr2[] = { new Student(2, "Robert"), new Student(3, "Adam") };

      // copies an array from the specified source array
      System.arraycopy(arr1, 0, arr2, 0, 1);
      System.out.print("array2 = ");

      for(int i= 0; i < arr2.length; i++) {      
         System.out.print(arr2[i] + " ");
      }
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}
Output

Let us compile and run the above program, this will produce the following result −

array2 = [ 1, Julie ] [ 3, Adam ] 

java_lang_system.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4