Java Copy Array - Array Copy in Java With Examples

Today we will look into different ways for java array copy. Java provides inbuilt methods to copy the array. Whether you want a full copy or partial copy of the array, you can do it easily using java inbuilt classes.

Java Copy Array

java copy array, array copy java
There are many ways to copy array in java. Let’s look at them one by one.

  1. Object.clone(): Object class provides clone() method and since array in java is also an Object, you can use this method to achieve full array copy. This method will not suit you if you want partial copy of the array.
  2. System.arraycopy(): System class arraycopy() is the best way to do partial copy of an array. It provides you an easy way to specify the total number of elements to copy and the source and destination array index positions. For example System.arraycopy(source, 3, destination, 2, 5) will copy 5 elements from source to destination, beginning from 3rd index of source to 2nd index of destination.
  3. Arrays.copyOf(): If you want to copy first few elements of an array or full copy of array, you can use this method. Obviously it’s not versatile like System.arraycopy() but it’s also not confusing and easy to use. This method internally use System arraycopy() method.
  4. Arrays.copyOfRange(): If you want few elements of an array to be copied, where starting index is not 0, you can use this method to copy partial array. Again this method is also using System arraycopy method itself.

So these are the four most useful methods for full and partial array copy. Note that all these inbuilt methods are to copy one-dimensional arrays only.

Java Copy Array Example

Now let’s see these java array copy methods in action with a simple java program.

Output of the above program is:

Java Array Copy – Shallow Copy

Note that all the inbuilt methods discussed above for array copy perform shallow copy, so they are good for primitive data types and immutable objects such as String. If you want to copy an array of mutable objects, you should do it by writing code for a deep copy yourself. Below is a program showing the problem with shallow copy methods.

Now when we run the above program, below is the output produced.

As you can see that empArray2 got changed too even though it was never the intention. This can cause serious issues in your application, so you are better of writing your own code for deep copying of array. Similar case is there for java object clone method.

Below is the code for full array copy to use in this case. You can easily write something like this for partial array copy too.

Now when we run the above program, the output produced is:

As you can see, in deep copy our source and destination array elements are referring to different objects. So they are totally detached and if we change one of them, the other will remain unaffected.

That’s all for java copy array and how to properly do array copy in java programs.

Reference: System class arraycopy documentation

By admin

Leave a Reply