Copy the content of an array (source) to another array (destination) in Java

Description

The following code shows how to copy the content of an array (source) to another array (destination).

System.arraycopy() copies the content of an array (source) to another array (destination), beginning at the specified position, to the specified position of the destination array.

Example

  
public class Main {
/*from www .j  a va 2s . c  o m*/
  public static void main(String[] a) {
    int[] array1 = { 1, 2, 3, 4 };
    int[] array2 = new int[array1.length];
    System.arraycopy(array1, 0, array2, 0, array1.length);

    for(int i: array2){
      
      System.out.println(i);
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    Java Data Type »




Java Boolean
Java Byte
Java Character
Java Currency
Java Double
Java Enum
Java Float
Java Integer
Java Long
Java Short
Java Auto Grow Array
Java Array Compare
Java Array Convert
Java Array Copy Clone
Java Array Fill
Java Array Search and Sort
Java String Convert
Java String File
Java String Format
Java String Operation
Java BigDecimal
Java BigInteger