English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about different methods that can be used to copy arrays (one-dimensional and two-dimensional) in Java with the help of examples.
In Java, we can copy one array to another. There are several techniques that can be used to copy arrays in Java.
Let's take an example
class Main { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5, 6}; int[] positiveNumbers = numbers; //Copy the array for (int number : positiveNumbers) { System.out.print(number + ""); } } }
Output:
1, 2, 3, 4, 5, 6
In the above example, we use the assignment operator (=) to copy an array named numbers to another array named positiveEnumbers.
This technique is the simplest one, and it is equally effective. However, this technique has a problem. If we change an element of one array, the corresponding element of the other array will also change. For example,
class Main { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5, 6}; int[] positiveNumbers = numbers; //Copy the array //Change the value of the first array numbers[0] = -1; //Print the second array for (int number : positiveNumbers) { System.out.print(number + ""); } } }
Output:
-1, 2, 3, 4, 5, 6
Here, we can see that we have changed the value of one element in the numbers array. When we print the positiveEnumbers array, we can see that the same value has also changed.
This is because both arrays reference the same array object. This is due to shallow copy. For more information on shallow copy, please visitShallow copy.
Now, to generate a new array object while copying the array, we need to perform a deep copy instead of a shallow copy.
Let's take an example:
import java.util.Arrays; class Main { public static void main(String[] args) { int[] source = {1, 2, 3, 4, 5, 6}; int[] destination = new int[6]; //Iterate and copy elements from the source to the destination for (int i = 0; i < source.length; ++i) { destination[i] = source[i]; } //convert the array to a string System.out.println(Arrays.toString(destination)); } }
Output:
[1, 2, 3, 4, 5, 6]
In the above example, we used a for loop to traverse each element of the source array. In each iteration, we copy the element from the source array to the destination array.
here, the source and target array references are different objects (deep copy). Therefore, if an element of an array is changed, the corresponding element of the other array will also remain unchanged.
Note the following statement,
System.out.println(Arrays.toString(destination));
here, the toString() method is used to convert the array to a string.
In Java,System classpackage contains a method named arraycopy() to copy arrays. Compared with the above two methods, this method is a better way to copy arrays.
method allows you to copy a specified part of the source array to the target array. For example,
arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
here,
src -the source array you want to copy
srcPos -the starting position (index) in the source array
dest -the target array, which will copy elements from the source
destPos -the starting position (index) in the target array
length -the number of elements to be copied
Let's take an example:
//using the Arrays.toString() method import java.util.Arrays; class Main { public static void main(String[] args) { int[] n1 = {2, 3, 12, 4, 12, -2}; int[] n3 = new int[5]; //create an array of length n1of n2array int[] n2 = new int[n1.length]; //the entire n1array is copied to n2 System.arraycopy(n1, 0, n2, 0, n1.length); System.out.println("n2 = " + Arrays.toString(n2)); //from n1the index of the array2copy element //the element will be copied to n3the index of the array1 //will be copied2elements System.arraycopy(n1, 2, n3, 1, 2); System.out.println("n3 = " + Arrays.toString(n3)); } }
Output:
n2 = [2, 3, 12, 4, 12, -2] n3 = [0, 12, 4, 0, 0]
In the above example, we used the arraycopy() method,
System.arraycopy(n1, 0, n2, 0, n1.length) - copy n1the entire element in the array is copied to n2in the array
System.arraycopy(n1, 2, n3, 1, 2 )- from index2starting at n1two elements of the array are copied from n3array1in the starting index
As you can see, the default initial value of an int type array element is 0.
We can also useJava Arraysdefined in the class to copy the array. For example,
//Using toString() and copyOfRange() methods import java.util.Arrays; class ArraysCopy { public static void main(String[] args) { int[] source = {2, 3, 12, 4, 12, -2}; //Copy the entire source array to the target int[] destination1 = Arrays.copyOfRange(source, 0, source.length); System.out.println("destination1 = " + Arrays.toString(destination1)); //from index2copy to5excluding5) int[] destination2 = Arrays.copyOfRange(source, 2, 5); System.out.println("destination2 = " + Arrays.toString(destination2)); } }
Output result
destination1 = [2, 3, 12, 4, 12, -2] destination2 = [12, 4, 12]
In the above example, please note the following line:
int[] destination1 = Arrays.copyOfRange(source, 0, source.length);
Here, we can see that we are creating destination1arrays and also copy the source array to it. We will not create destination before calling the copyOfRange() method1For more information about this method, please visitJava copyOfRange.
Similar to a one-dimensional array, we can also use a for loop to copy a two-dimensional array. For example,
import java.util.Arrays; class Main { public static void main(String[] args) { int[][] source = { {1, 2, 3, 4}, {5, 6}, {0, 2, 42, -4, 5} }; int[][] destination = new int[source.length][]; for (int i = 0; i < destination.length; ++i) { //Allocate space for each row of the target array destination[i] = new int[source[i].length]; for (int j = 0; j < destination[i].length; ++j) { destination[i][j] = source[i][j]; } } //Display the target array System.out.println(Arrays.deepToString(destination)); } }
Output:
[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]
In the above program, please note the following line:
System.out.println(Arrays.deepToString(destination);
Here, the deepToString() method is used to provide a better representation of a two-dimensional array. For more information, please visitJava deepToString().
To simplify the above code, we can use System.arraycopy() to replace the inner loop, just like handling a one-dimensional array. For example, for example,
import java.util.Arrays; class Main { public static void main(String[] args) { int[][] source = { {1, 2, 3, 4}, {5, 6}, {0, 2, 42, -4, 5} }; int[][] destination = new int[source.length][]; for (int i = 0; i<source.length; ++i) { //Allocate space for each row of the target array destination[i] = new int[source[i].length]; System.arraycopy(source[i], 0, destination[i], 0, destination[i].length); } //Display the target array System.out.println(Arrays.deepToString(destination)); } }
Output:
[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]
Here, we can see that by replacing the internal for loop with the arraycopy() method, the same output can be obtained.