Copying an Array

Let's discuss how we can create a copy of an array using the copyOf() method.

If we need to create copies of our array, then we can use the copyOf() method from the Arrays class. We need to provide the array that needs to be copied and the new array’s size as a parameter.

The below example shows how to create a copy of an array where the copied array size is the same as the original array. If the new array’s size is greater than the original array, then the remaining positions are filled with zeros.

Get hands-on with 1200+ tech skills courses.