copyOf() & fill()
Arrays.copyOf(arr, newLen) copies and resizes (pads with 0/null/false). Arrays.copyOfRange(arr, from, to) extracts a subarray. Arrays.fill(arr, val) initializes every element; Arrays.fill(arr, from, to, val) fills a subrange.
How It Works
Arrays.copyOf(arr, newLen) allocates a fresh array of the requested length and copies elements over, padding with zeros, false, or null when the new length exceeds the original — the idiomatic way to 'resize' a fixed-length Java array. Arrays.copyOfRange(arr, from, to) extracts a subarray with an inclusive from and exclusive to, and the to index may legally exceed the source length, padding the tail.
Arrays.fill(arr, val) overwrites every slot with one value, with a from/to overload for subranges — the standard way to initialize DP tables or reset visited markers. All of these are O(n) in the elements touched, and copies are shallow: for object arrays the references are duplicated, not the objects they point to.
Step-by-Step Visualization
Code
int[] arr = {1, 2, 3, 4, 5};
// ─── copyOf ───────────────────────────────────────────────────
Arrays.copyOf(arr, 3); // [1, 2, 3] — truncate
Arrays.copyOf(arr, 8); // [1,2,3,4,5,0,0,0] — pad with 0s
// ─── copyOfRange (from inclusive, to exclusive) ───────────────
Arrays.copyOfRange(arr, 1, 4); // [2, 3, 4]
Arrays.copyOfRange(arr, 3, 10); // [4,5,0,0,0,0,0] — pads if to > length
// ─── fill ─────────────────────────────────────────────────────
int[] dp = new int[6];
Arrays.fill(dp, Integer.MAX_VALUE); // all cells = MAX (DP sentinel)
Arrays.fill(dp, 1, 4, 0); // fill subrange [1, 4) with 0
int[][] grid = new int[3][4];
for (int[] row : grid) Arrays.fill(row, -1); // 2D: fill row by row
// ─── System.arraycopy (faster bulk copy) ─────────────────────
System.arraycopy(arr, 0, dest, 0, 5); // (src, srcPos, dst, dstPos, len)Tips & Gotchas
Practice Problems
- 1Merge Sorted Array
- 2Rotate Array
- 3Coin Change
- 4Range Addition
About the Arrays Utility API Pattern
java.util.Arrays methods and array ↔ collection conversion patterns. These utilities handle sorting, searching, copying, filling, and bridging between primitive arrays and Java collections.
When you see 'subarray', 'contiguous', or 'in-place', think arrays. The key is reducing brute-force O(n²) to O(n) using sliding window, two pointers, or prefix sums.
Common Array Interview Problems
- Two Sum
- Best Time to Buy & Sell Stock
- Maximum Subarray
- Merge Intervals
- Product of Array Except Self
- Container With Most Water
Frequently Asked Questions
What is the difference between copyOf and clone for arrays?
arr.clone() always duplicates the array at its exact current length, while Arrays.copyOf lets you grow or truncate in the same call. Both are shallow copies, so for a 2D array you must copy each row individually or the clones will share row references.
How do I fill a 2D array with a value?
Arrays.fill only works on one dimension, so loop over the rows and call Arrays.fill(row, val) on each. Beware of initializing rows via reference assignment — every row would alias the same array, and writing one cell would appear to write a whole column.