Java

How to Copy an Array in Java

Arrays are the basic programming component utilized for storing a large amount of data. We can also copy as well as replace the elements of an array with another array. More specifically, Java provides multiple methods to copy the elements of an array, including the “Iteration” approach, “arraycopy()” method, and “copyofRange()” method.

This post will explain the different methods for copying an array in Java.

How to Copy an Array in Java?

To copy an array in Java, we will discuss the following approaches:

Method 1: Copy an Array in Java Using Iteration Approach

In this method, we will iterate each element of the stated original array and copy one element at a time. With the usage of this method, elements can be copied to another array for manipulation. Furthermore, the original array does not get affected.

Example

Here, first of all, create an array and insert elements inside the array:

int x[] = { 8, 5, 9 };

Then, create another array of the same size as by getting the length of the first array:

int y[] = new int[x.length];

Utilize the “for” loop for the iteration and set the value of the second array’s index equal to the first array:

for (int i = 0; i < x.length; i++)

  y[i] = x[i];

Then, increment the index value of the second array:

y[0]++;

Utilize the “println()” to display the elements of the first array on the console:

System.out.println("Elements of array x");

Now, iterate the array “x” and print all elements on the console using the “for” loop:

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

Similarly, iterate the array “y” by utilizing the same method:

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


It can be observed that the elements of the first have been copied successfully:

Method 2: Copy an Array in Java Using “arraycopy()” Method

You can also copy an array with the help of the “arraycopy()” Method. This method is utilized for creating a copy with a range of elements from an array that are starting at the first element and pasting them into another array starting at the first element. The length is defined as an integer 32-bit.

To utilize the “copyarray()” method, follow the given syntax:

arraycopy(Object src, srcPos, Object dest, destPos, length)

Here:

  • src” defines the source of the array.
  • srcPos” specifies the index from where the copying of the element started.
  • dest” defines the destination of the array.
  • destPos” indicates the index where copied elements are pasted in the destination array.
  • length” is utilized to specify the subarray length that needs to be copied.

Example

Invoke the “arraycopy()” method and set the source of the array and other parameters according to the need:

System.arraycopy(x, 0, y, 0, 3);

Output

Method 3: Copy an Array in Java Using “copyofRange” Method

The “copyofRange()” method copies the particular range of the specified array into a new array. To do so, the syntax of this method is defined below:

copyOfRange​(int[] original, int from, int to)

According to the given syntax:

  • original” indicates the original array.
  • from” is used to copy the element from the specified index.
  • to” defines the index to which the array elements are copied.

Example

To utilize the “copyOfRange()” method, import the “java.util.Arrays” library:

import java.util.Arrays;

Define the array with a particular name and store the elements of the array:

int x[] = { 2, 9, 5, 8, 15, 18 };

Next, initialize another array and invoke the “copyOfRange()” method. Then, pass the arguments to set the range for copying the elements:

int y[] = Arrays.copyOfRange(x, 2, 6);

According to the given code, the elements from the second index to the sixth index have been copied successfully from the “x” to “y” array:

That’s all about copying an array in Java with different methods.

Conclusion

To copy an array in Java, there are multiple methods that can be used, including the “Iteration Approach”, “arraycopy()” method, and “copyofRange()” method. More specifically, the “copyarray()” method is used to copy a specified range of elements from a defined starting and ending index. This post stated the methods for copying an array in Java.

About the author

Hafsa Javed