1. Introduction to Array Copying

Premium Video

This video is available to Premium members or users who have purchased the course.

Sign in to Unlock

Sometimes we need a copy of an array so that we can work with the copied data separately. Java provides several ways to copy arrays, including:

  • Assignment
  • A for loop
  • System.arraycopy()
  • Arrays.copyOf()
  • Arrays.copyOfRange()
  • clone()

The most important distinction is that assigning one array variable to another does not create a new array. It copies the reference, so both variables refer to the same array object.

Learning objectives

After reading this article, you will be able to:

  • Explain the difference between assignment and copying.
  • Copy array elements manually with a loop.
  • Use System.arraycopy() to copy elements.
  • Create full or resized copies with Arrays.copyOf().
  • Copy part of an array with Arrays.copyOfRange().
  • Use clone() to copy an array.
  • Choose a suitable method for a basic array-copying task.

2. Copying an Array Using Assignment

Consider:

This does not create a second array. The variable copy refers to the same array as numbers.

Complete runnable example

Expected output

numbers[0]: 100copy[0]: 100

How it works

Both variables point to the same array. Therefore, changing an element through copy is visible through numbers as well.

Remember: Assignment copies an array reference, not the array's elements into a new array.

3. Copying an Array Using a for Loop

You can create a new array and copy each element manually. This approach makes the copying process easy to understand and gives you control over which elements are copied.

Complete runnable example

Expected output

Original: [10, 20, 30]Copy: [100, 20, 30]

How it works

  1. new int[numbers.length] creates a separate array with the same length.
  2. The loop visits each valid index.
  3. copy[i] = numbers[i] transfers each value into the new array.
  4. Updating copy[0] does not change numbers[0].

For a one-dimensional primitive array, this creates an independent array of values.

4. Copying with System.arraycopy()

Java provides System.arraycopy() to copy a selected number of elements from one array into another array.

The method's parameter order is:

  • source: the array to copy from.
  • sourcePosition: the index in the source where copying begins.
  • destination: the array to copy into.
  • destinationPosition: the index in the destination where copied values begin.
  • numberOfElements: how many elements to copy.

Complete runnable example

Expected output

 [10, 20, 30, 40, 50]

Copying only part of an array

Expected output

 [20, 30, 40]

Here, copying starts at source index 1, and the three copied elements are placed starting at destination index 0.

5. Copying with Arrays.copyOf()

The Arrays.copyOf() method creates a new array and copies elements from the original array up to the requested length. Import java.util.Arrays to use it.

Complete runnable example

Expected output

Original: [10, 20, 30, 40, 50]Copy: [10, 20, 30, 40, 50]

This is a concise way to create a new array with the same length and copied values.

6. Copying an Array with a Different Size

One advantage of Arrays.copyOf() is that you can choose a new length.

Complete runnable example

Expected output

 [10, 20, 30, 0, 0]

The original array has three elements, but the new array has five. The extra positions in an int[] are initialized to 0.

If the requested length is shorter than the original array, only the elements that fit in the new array are copied.

7. Copying a Range with Arrays.copyOfRange()

Arrays.copyOfRange() creates a new array containing a selected range of elements.

The basic syntax is:

The from index is included, while the to index is excluded.

Complete runnable example

Expected output

 [20, 30, 40]

The copied positions are indexes 1, 2, and 3, which contain 20, 30, and 40.

8. Copying an Array Using clone()

Arrays provide a clone() method that creates a new array with the same length and copies its elements.

Complete runnable example

Expected output

Original: [10, 20, 30]Copy: [100, 20, 30]

For this one-dimensional primitive array, the cloned array is separate from the original, so changing one array's elements does not change the other's elements.

Important distinction: For arrays of objects, clone() copies the array structure and the element references. It does not automatically clone each object inside the array. This is called a shallow copy.

9. Assignment vs. an Actual Array Copy

This is one of the most important differences in Java array handling.

CodeCreates a new array?What happens
int[] copy = numbers;NoBoth variables refer to the same array
Manual for loop into a new arrayYesValues are copied one by one
System.arraycopy(...) into a new destinationDestination must existCopies elements into that destination
Arrays.copyOf(numbers, numbers.length)YesCreates a new array and copies values/references
Arrays.copyOfRange(numbers, from, to)YesCreates a new array for the selected range
numbers.clone()YesCreates a new array with copied elements/references

For primitive arrays, a new array copy keeps the primitive values independent. For arrays holding objects or nested arrays, these methods make a shallow copy: the outer array is new, but its elements may still refer to the same objects or inner arrays.

10. Flowchart: Creating an Independent Array Copy

The flowchart below describes the manual copying method using a for loop.

Whiteboard
Loading diagram...


Flowchart explanation

  1. Begin with the original array.
  2. Create a destination array with enough space for the elements you want to copy.
  3. Initialize the index to 0.
  4. Check whether the index is still within the original array's length.
  5. Copy the current element to the same index in the destination array.
  6. Increase the index and repeat.
  7. When every element has been copied, use the new array.

11. Complete Example: Copy and Modify an Array

This example demonstrates that changing a copied primitive array does not change the original array.

Expected output

Original: [10, 20, 30, 40, 50]
Copy: [100, 20, 30, 40, 50]

The original array stays unchanged because Arrays.copyOf() created a separate array object.

12. Common Mistakes and Best Practices

Mistake 1: Thinking assignment creates a new array

This copies the reference, not the array. Use a copying method if you need a separate array object.

Mistake 2: Copying into a destination that is too small

With System.arraycopy(), make sure the destination has enough available positions for the requested number of elements. Otherwise, an exception can occur.

Mistake 3: Misunderstanding the end index

In Arrays.copyOfRange(numbers, from, to), from is included and to is excluded.

Mistake 4: Assuming object arrays are deeply copied

Array-copying methods generally copy element references for object arrays. If independent copies of the objects are needed, those objects must be copied separately using an appropriate strategy.

Best practices

  • Use Arrays.copyOf() for a simple full-array copy.
  • Use Arrays.copyOfRange() when only a range is needed.
  • Use System.arraycopy() when you need control over source and destination positions.
  • Use a for loop when you want to understand or customize the copy process.
  • Use clone() for a straightforward array-level copy.
  • Do not use assignment when the goal is to create an independent array.
  • Remember that a shallow copy of an array containing objects does not duplicate the objects themselves.

13. Practice Questions with Answers

Question 1: Does int[] b = a; create a new array?

Answer: No. Both variables refer to the same array object.

Question 2: Which method is convenient for copying an array to a specified length?

Answer: Arrays.copyOf().

Question 3: What is the output?

Answer:

 9

Both variables refer to the same array.

Question 4: What is the output?

Answer:

29

The arrays are separate, so changing b does not change a.

Question 5: What does Arrays.copyOfRange(a, 1, 3) copy?

Answer: It copies the elements at indexes 1 and 2; the end index 3 is excluded.

Question 6: What is a shallow copy?

Answer: A shallow copy creates a new outer array but copies references to contained objects or inner arrays rather than duplicating those objects or arrays.

Question 7: Write a program that copies an array using a for loop.

Answer:

Expected output

 [5, 10, 15, 20]

14. Frequently Asked Questions (FAQs)

1. What is array copying in Java?

Array copying means creating another array and transferring elements from an existing array into it.

2. Does assignment copy an array?

No. Assignment copies the reference, so both variables refer to the same array.

3. Which array-copying method is easiest for a full copy?

Arrays.copyOf(original, original.length) is a convenient option.

4. What is the difference between System.arraycopy() and Arrays.copyOf()?

System.arraycopy() copies a specified number of elements into an existing destination array. Arrays.copyOf() creates a new array of a specified length and copies elements into it.

5. Can Arrays.copyOf() increase an array's length?

Yes. Any added positions contain the default value for the component type, such as 0 for int.

6. Is Arrays.copyOfRange()'s end index included?

No. The start index is included, but the end index is excluded.

7. Does clone() create a separate array?

Yes, it creates a new array object. However, for object arrays, the contained object references are copied rather than the objects themselves.

8. What is the difference between a shallow copy and a deep copy?

A shallow copy duplicates the outer array but not the referenced objects or nested arrays. A deep copy also creates independent copies of the contained objects or nested structures, according to the chosen copying logic.

15. Quick Revision

  • Assignment (copy = numbers) copies a reference, not the array.
  • A for loop can copy elements one by one into a new array.
  • System.arraycopy() copies elements into an existing destination array.
  • Arrays.copyOf() creates a new array with a chosen length.
  • Arrays.copyOfRange() copies a selected range; the end index is exclusive.
  • clone() creates a new array object.
  • For primitive arrays, values in a copied array are independent of the original.
  • For arrays of objects or nested arrays, these techniques generally make shallow copies.
  • Choose the method based on whether you need a full copy, a range, or control over positions.

16. Conclusion

Java offers several ways to copy arrays, and each method suits a different situation. Assignment only creates another reference to the same array, while a loop, System.arraycopy(), Arrays.copyOf(), Arrays.copyOfRange(), or clone() can create a new array object. For beginner-friendly full copies, Arrays.copyOf() is often a convenient choice. Always remember the difference between a shallow copy and a deep copy when arrays contain objects or other arrays.