Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Copy an Array in Java,

you can use various approaches depending on your requirements. Here are a few methods commonly used to copy arrays:

  1. Using a Loop: You can manually iterate over the elements of the source array and copy them to a new array.

    int[] sourceArray = {1, 2, 3, 4, 5};
    int[] destinationArray = new int[sourceArray.length];
    
    for (int i = 0; i < sourceArray.length; i++) {
        destinationArray[i] = sourceArray[i];
    }
  2. Using System.arraycopy(): The System.arraycopy() method can be used to efficiently copy elements between arrays.

    int[] sourceArray = {1, 2, 3, 4, 5};
    int[] destinationArray = new int[sourceArray.length];
    
    System.arraycopy(sourceArray, 0, destinationArray, 0, sourceArray.length);
  3. Using Arrays.copyOf(): The Arrays.copyOf() method creates a new array and copies the specified length of elements from the source array.

    int[] sourceArray = {1, 2, 3, 4, 5};
    int[] destinationArray = Arrays.copyOf(sourceArray, sourceArray.length);
  4. Using Arrays.copyOfRange(): The Arrays.copyOfRange() method copies a specific range of elements from the source array to a new array.

    int[] sourceArray = {1, 2, 3, 4, 5};
    int[] destinationArray = Arrays.copyOfRange(sourceArray, 0, sourceArray.length);

All of the above methods create a new array that contains the copied elements from the source array. Remember that these methods perform a shallow copy, meaning that if the source array contains reference types (objects), the copied array will hold references to the same objects. If you need to perform a deep copy, where the objects are also cloned, you'll need to handle that separately.

Choose the appropriate method based on your needs and the simplicity of the code you prefer.