Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 17, 2022 10:44 am GMT

Multiple Ways to Copy Array in Java Effectively

If you were given the task of copying the values from array a to array b, how would you do it?

Thinking man

...if you asked me when I was a beginner in java programming, I would have answered it as in Source code 1.

Source code 1:

int[] a = {1, 2, 3, 4, 5};int[] b = a;System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 1:

Array a: [1, 2, 3, 4, 5]Array b: [1, 2, 3, 4, 5]

With Source code 1, this task is certainly successful because I can perfectly copy the values from array a to array b with the proof can be seen in Output 1.

But what if something causes one of the values of array a to change? as in Source code 2, the first value in array a changes to something else.

Source code 2:

int[] a = {1, 2, 3, 4, 5};int[] b = a;a[0] = 2;System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 2:

Array a: [2, 2, 3, 4, 5]Array b: [2, 2, 3, 4, 5]

You can see the result in Output 2, where the value of array b will also change if there is a change in the value of array a. This method is not recommended for copying the values of one array to another (unless you intentionally want to link the values of the two arrays).

Well then, but how do I copy instead of linking the values of array a to array b?
...One way is to individually copy value from array a to value from array b using loops, as can be seen in Source code 3.

Source code 3:

int[] a = {1, 2, 3, 4, 5};int[] b = new int[a.length];for (int i = 0; i < a.length; i++) {    b[i] = a[i];}System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 3:

Array a: [1, 2, 3, 4, 5]Array b: [1, 2, 3, 4, 5]

You can see that using the code in Source code 3, I can make array b and array a have the same values as in Output 3.

Then, what if I update one of the values in array a as shown in Source code 4?

Source code 4:

int[] a = {1, 2, 3, 4, 5};int[] b = new int[a.length];for (int i = 0; i < a.length; i++) {    b[i] = a[i];}a[0] = 2;System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 4:

Array a: [2, 2, 3, 4, 5]Array b: [1, 2, 3, 4, 5]

As Output 4 shows, array b will not change if there is an update to any of the values in array a. Congratulations, you have successfully copied the value of array a to array b!

But we will not stop here, because it turns out that Java has made it easy to copy array values to other arrays without having to use the iterative method.

There are 4 built-in methods you can use to copy arrays effectively in Java.

  • copyOf()
  • copyOfRange()
  • arraycopy()
  • clone()

And I will try to explain the four methods one by one.

1. Using copyOf() method

copyOf() is the first method that can be used to copy the values of one array to another. Syntax 1 is the syntax for this method:

Syntax 1:
public static int[] copyOf(int[] original, int newLength)

  • original is the source array.
  • newLength is the length of the destination array.

Source code 5 shows the implementation of this method.

Source code 5:

int[] a = {1, 2, 3, 4, 5};int[] b = Arrays.copyOf(a, a.length);System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 5:

Array a: [1, 2, 3, 4, 5]Array b: [1, 2, 3, 4, 5]

You can see the complexity of the code can be reduced compared to the "manual iteration" method I described earlier. And at Output 5, it can be seen that the results that appear are also in accordance with what we expected.

Source code 6 shows the code in which one of the array values a is modified, and the result can be seen in Output 6.

Source code 6:

int[] a = {1, 2, 3, 4, 5};int[] b = Arrays.copyOf(a, a.length);a[0]++;System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 6:

Array a: [2, 2, 3, 4, 5]Array b: [1, 2, 3, 4, 5]

If you are interested in this method, and want to learn more about it, you can start by studying the documentation for the class that this method belongs to, namely Arrays. Here is a link to the Arrays class documentation: https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

2. Using copyOfRange() method

copyOfRange() is an expansion of the previous method, where this method allows us to specify the range of array values that we will copy. And this method also comes from the same class, namely the Arrays class. The syntax of this method can be seen in Syntax 2.

Syntax 2:
public static int[] copyOfRange(int[] original, int from, int to)

  • original is the source array.
  • from is the start index value to be copied.
  • to is the final index value to be copied.

Source code 7 shows the implementation of this method.

Source code 7:

int[] a = {1, 2, 3, 4, 5};int[] b = Arrays.copyOfRange(a, 0, a.length);System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 7:

Array a: [1, 2, 3, 4, 5]Array b: [1, 2, 3, 4, 5]

Output 7 shows that the results are in line with our expectations.

Source code 8 is code where one of the values of array a is changed and Output 8 shows that the value of array b is not affected by changes in the value of array a.

Source code 8:

int[] a = {1, 2, 3, 4, 5};int[] b = Arrays.copyOfRange(a, 0, a.length);a[0]++;System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 8:

Array a: [2, 2, 3, 4, 5]Array b: [1, 2, 3, 4, 5]

3. Using arraycopy() method

arraycopy() is the third method for copying the values of one array to another. This method is also the foundation of the implementation of the copyOf and copyOfRange methods which if you look at the implementation code of these methods in the Arrays class, both methods use the arraycopy() method. The syntax of this method can be seen in Syntax 3.

Syntax 3:
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length);

  • src is the source array
  • srcPos is starting position in the source array.
  • dest is the destination array.
  • destPos is starting position in the destination data.
  • length is the number of array elements to be copied.

Source code 9 shows the implementation of this method.

Source code 9:

int[] a = {1, 2, 3, 4, 5};int[] b = new int[a.length];System.arraycopy(a, 0, b, 0, a.length);System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 9:

Array a: [1, 2, 3, 4, 5]Array b: [1, 2, 3, 4, 5]

As in the code above, we must initialize array b first. We cannot directly assign array b with the arraycopy() method, because this method is void, so this method does not return any value. After that we will call the arraycopy() method, to copy the value of array a to array b.

The results of this code can be seen in Output 9, where it can be seen that the results are in line with our expectations.

Source code 10 is the code where if one of the values of array a is updated, and Output 10 is the result of the code where array b is not affected by the update of array a.

Source code 10:

int[] a = {1, 2, 3, 4, 5};int[] b = new int[a.length];System.arraycopy(a, 0, b, 0, a.length);a[0]++;System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 10:

Array a: [2, 2, 3, 4, 5]Array b: [1, 2, 3, 4, 5]

If you are interested in learning more about this method, you can start from the implementation class of this method, the System class. Here is the System class documentation: https://docs.oracle.com/javase/7/docs/api/java/lang/System.html

4. Using clone() method

clone() method is my favorite method because it is very easy to use, and very simple. To be able to use this method, you only need to call the clone method of the object array to be copied.

The implementation of this method can be seen in Source code 11.

Source code 11:

int[] a = {1, 2, 3, 4, 5};int[] b = a.clone();System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 11:

Array a: [1, 2, 3, 4, 5]Array b: [1, 2, 3, 4, 5]

It can be seen from Output 11, the results of the code above are in line with our expectations.

And finally in Source code 12, it can be seen that there is an update of one of the values of array a. And in Output 12, it can be seen that the value of array b is not affected by changes that occur in array a.

Source code 12:

int[] a = {1, 2, 3, 4, 5};int[] b = a.clone();a[0]++;System.out.println("Array a: " + Arrays.toString(a));System.out.println("Array b: " + Arrays.toString(b));

Output 12:

Array a: [2, 2, 3, 4, 5]Array b: [1, 2, 3, 4, 5]

Those are some of my explanations of some of the built-in Java methods to copy one array to another more effectively. Note that the operations of the above methods, apart from integers, can also be performed on arrays with other datatypes such as double, boolean, etc.

Image cover:
https://i.picsum.photos/id/116/1920/720.jpg?hmac=efKDwhyr2wd5_mn1XOD1sjLYVawieQjikrwTWmsP5-U

Other image:
https://toppng.com/uploads/preview/thinking-man-11530959704arfgepmc9x.png


Original Link: https://dev.to/luthfisauqi17/multiple-ways-to-copy-array-in-java-effectively-4lpj

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To