blog posts

How To Copy Java Arrays (In Very Simple Language)

In this tutorial, you will learn about different methods that you can use to copy arrays (both one-dimensional and two-dimensional) in Java.

There are several techniques you can use to copy arrays in Java.

Copy arrays using the assignment operator

Let’s take an example,

  1. class CopyArray {
  2. public static void main (String [] args) {
  3. int [] numbers = {1, 2, 3, 4, 5, 6};
  4. int [] positiveNumbers = numbers; // copying arrays
  5. for (int number: positiveNumbers) {
  6. System.out.print (number + “,“);
  7. }
  8. }
  9. }

Output

1, 2, 3, 4, 5, 6

Although this method seems to be quite efficient for copying arrays, there is a problem with this.

If you change the elements of one array in the example above, the elements of the other array will also change.

  1. class AssignmentOperator {
  2. public static void main (String [] args) {
  3. int [] numbers = {1, 2, 3, 4, 5, 6};
  4. int [] positiveNumbers = numbers; // copying arrays
  5. numbers [0] = -1;
  6. for (int number: positiveNumbers) {
  7. System.out.print (number + “,“);
  8. }
  9. }
  10. }

Output

-1, 2, 3, 4, 5, 6

When the first element of the numbers array changes to -1, the first element of the positiveNumbers array also changes to -1. Because both arrays point to the same array object.

This is called surface copy.

Most of the time, though, we need deep copy instead of superficial copy. A deep copy copies the creation values ​​of the new array object.

2- Using the loop structure to copy arrays

Let’s take an example:

  1. import java.util.Arrays;
  2. class ArraysCopy {
  3. public static void main (String [] args) {
  4. int [] source = {1, 2, 3, 4, 5, 6};
  5. int [] destination = new int [6];
  6. for (int i = 0; i <source.length; ++ i) {
  7. destination [i] = source [i];
  8. }
  9. // converting array to string
  10. System.out.println (Arrays.toString (destination));
  11. }
  12. }

Output

[1, 2, 3, 4, 5, 6]

Here, the loop is used to copy the elements of the source array. In each iteration, the corresponding element of the source array is copied to the destination array.

Source and destination arrays do not have the same reference (deep copy). This means that if the elements of one array (or source or destination) change, the elements of the other array will not change.

The toString () method is used to convert arrays to strings (for output only).

There is a better way (better than loops) to copy arrays in Java using arraycopy () and copyOfRange ().

3. Copy arrays with arraycopy ()

The System class contains an arraycopy () method that allows you to copy data from one array to another.

The arraycopy () method is efficient and flexible. This method allows you to copy a specific part of the source array to the destination array .

public static void arraycopy (Object src, int srcPos,

Object dest, int destPos, int length)

here,

  • src – The array you want to copy
  • srcPos – Start position (index) in src array
  • dest – The elements of the src array are copied to this array
  • destPos – Start position (index) in the destination array
  • length – The number of elements to copy

Let’s take an example:

  1. // To use Arrays.toString () method
  2. import java.util.Arrays;
  3. class ArraysCopy {
  4. public static void main (String [] args) {
  5. int [] n1 = {2, 3, 12, 4, 12, -2};
  6. int [] n3 = new int [5];
  7. // Creating n2 array of having length of n1 array
  8. int [] n2 = new int [n1.length];
  9. // copying entire n1 array to n2
  10. System.arraycopy (n1, 0, n2, 0, n1.length);
  11. System.out.println (“n2 =” + Arrays.toString (n2));
  12. // copying elements from index 2 on n1 array
  13. // copying element to index 1 of n3 array
  14. // 2 elements will be copied
  15. System.arraycopy (n1, 2, n3, 1, 2);
  16. System.out.println (“n3 =” + Arrays.toString (n3));
  17. }
  18. }

Output

n2 = [2, 3, 12, 4, 12, -2]

n3 = [0, 12, 4, 0, 0]

Note that the default initial value of the elements of an array is integer 0.

4- Copy arrays with copyOfRange () method

Additionally, you can use the copyOfRange () method defined in the java.util.Arrays class to copy arrays. You do not need to create a destination array before using this method.

Here’s how to do it.

  1. // To use toString () and copyOfRange () method
  2. import java.util.Arrays;
  3. class ArraysCopy {
  4. public static void main (String [] args) {
  5. int [] source = {2, 3, 12, 4, 12, -2};
  6. // copying entire source array to destination
  7. int [] destination1 = Arrays.copyOfRange (source, 0, source.length);
  8. System.out.println (“destination1 =” + Arrays.toString (destination1));
  9. // copying from index 2 to 5 (5 is not included)
  10. int [] destination2 = Arrays.copyOfRange (source, 2, 5);
  11. System.out.println (“destination2 =” + Arrays.toString (destination2));
  12. }
  13. }

Output

destination1 = [2, 3, 12, 4, 12, -2]

destination2 = [12, 4, 12]

Copy two-dimensional arrays using loops

There is an example of copying irregular two-dimensional arrays using loops:

  1. import java.util.Arrays;
  2. class ArraysCopy {
  3. public static void main (String [] args) {
  4. int [] [] source = source
  5. {1, 2, 3, 4},
  6. {5, 6},
  7. ۰ 0, 2, 42, -4, 5.
  8. };
  9. int [] [] destination = new int [source.length] [];
  10. for (int i = 0; i <destination.length; ++ i) {
  11. // allocating space for each row of destination array
  12. destination [i] = new int [source [i] .length];
  13. for (int j = 0; j <destination [i] .length; ++ j) {
  14. destination [i] [j] = source [i] [j];
  15. }
  16. }
  17. // displaying destination array
  18. System.out.println (Arrays.deepToString (destination));
  19. }
  20. }

Output

[[1, 2, 3, 4], [5, 6], [0, 2, 42, -4, 5]]

In the above code, we used the Arrays.deepToString () method. deepToString () provides a better view of a multidimensional array like a 2D array.

You can replace the inner loop of the above code with System.arraycopy () or Arrays.copyOf () like a one-dimensional array.

Here is an example of doing the same thing with arraycopy ().

  1. import java.util.Arrays;
  2. class AssignmentOperator {
  3. public static void main (String [] args) {
  4. int [] [] source = source
  5. {1, 2, 3, 4},
  6. {5, 6},
  7. ۰ 0, 2, 42, -4, 5.
  8. };
  9. int [] [] destination = new int [source.length] [];
  10. for (int i = 0; i <source.length; ++ i) {
  11. // allocating space for each row of destination array
  12. destination [i] = new int [source [i] .length];
  13. System.arraycopy (source [i], 0, destination [i], 0, destination [i] .length);
  14. }
  15. // displaying destination array
  16. System.out.println (Arrays.deepToString (destination));
  17. }
  18. }