Java Copy Arrays Tutorial

In this section, we will learn the methods by which we can copy the elements of an array and create a new array as a result.

Copy Arrays via Assignment Operator

The first method of passing arrays is not actually copying an array but simply passing a reference!

Basically, when you pass a variable that is holding an array to another variable, all we do here is passing the memory address of the target array in the memory to another variable.

So at the end, both variables are pointing to the same array and if one changed the elements of the array, the other will see.

Example: copy arrays via assignment operator

class Main{
	public static void main(String[] args) {
		int []arr = {1,2,3,4,5,6};
		int []arr2 = arr; 
		arr[0] = 1000; 
		System.out.println(arr2[0]);
	}
}
Output: 
1000 

Copy Arrays via loops

This method of copying the elements of an array and passing it to another array is basically done by iterating through the elements of one array using loops like `for-loop`, take each element of the first array and then pass them as the elements of another array.

Note: if the purpose is to pass the entire elements of one array to another, then the second array must have at least the same size as the first array.

Example: copy arrays via for-each loop

class Main{
	public static void main(String[] args) {
		int []arr = {1,2,3,4,5,6};
		int []arr2 = new int[arr.length]; 
		for (int i = 0; i<arr.length; i++){
			arr2[i] = arr[i];
		}
		for (int i: arr2){
			System.out.print(i+", ");
		}
	}
}
Output: 
1, 2, 3, 4, 5, 6,

Copy Arrays via System.arraycopy() method

Using the arraycopy() method of the System class, we can copy the entire elements or a specific range of elements of an array and pass them as the elements of another array.

Java System.arraycopy() method Syntax:

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

The method takes 5 arguments:

– The first argument is a reference to the array we want to copy its elements and pass them to another array.

– The second argument is the index position where we want this copy to start from the source array.

– The third argument is the target array we want to pass the elements to.

– The fourth argument is the index position where the copied elements should be placed from that position onward.

– The fifth and the last argument is the number of elements that should be copied from the source array and be passed to the destination array.

Example: using the System.arraycopy() method

class Main{
	public static void main(String[] args) {
		int []arr = {1,2,3,4,5,6};
		int []arr2 = new int[arr.length]; 
		System.arraycopy(arr,0,arr2,0,arr.length);
		for(int i: arr2){
			System.out.print(i+", ");
		}
	}
}
Output: 
1, 2, 3, 4, 5, 6,

Copy Arrays via Arrays.copyOf() method

Another way of copying the elements of an array and pass them as the argument to another array is by using the Arrays copyOf() method.

Note: this method will create a new array from the original array and return that as a result.

Java Arrays.copyOf() Method Syntax:

copyOf(int[] original, int newLength)

The method takes two arguments:

– The first argument is the original or the source array we want to take its elements.

– The second argument is the size of the new array. This means if the original array has 10 elements already and we set the second argument of this method to 5, the size of the new array becomes 5 and then the first 5 elements of the original array will be passed to the copied array. 

Example: using the Arrays.copyOf() method in Java

import java.util.Arrays;
class Main{
	public static void main(String[] args) {
		int []arr = {1,2,3,4,5,6};
		int []arr2 = Arrays.copyOf(arr,3); 

		for(int i: arr){
			System.out.print(i+", ");
		}
		System.out.println("");
		for(int i: arr2){
			System.out.print(i+", ");
		}
	}
}
Output: 
1, 2, 3, 4, 5, 6,
1, 2, 3,

Copy Arrays via Arrays.copyOfRange() method

Another way of copying the elements of an array to another array is by using the copyOfRange() method.

Note: this method also returns a new array from the original array.

Java Arrays.copyOfRange() Method Syntax:

public static data-type[] copyOfRange(data-type[] original, int from, int to)

The method takes 3 arguments:

– The first argument is a reference to the original array that we want to copy its elements.

– The second argument is the index number where we want to start copying the elements from that position of the original array.

– The third argument is the index number where we want to stop copying elements of the target array. (This value is exclusive)

Example: using the Arrays.copyOfRange() method in Java

import java.util.Arrays;
class Main{
	public static void main(String[] args) {
		int []arr = {1,2,3,4,5,6};
		int []arr2 = Arrays.copyOfRange(arr,0,4); 

		for(int i: arr2){
			System.out.print(i+", ");
		}
	}
}
Output: 
1, 2, 3, 4,

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies