Mastering Java Array Utility Methods – sort(), binarySearch(), copyOf() and More

Illustration for Mastering Java Array Utility Methods – sort(), binarySearch(), copyOf() and More
By Last updated:

Introduction

Arrays are a fundamental data structure in Java, but working with them efficiently requires more than just declaration and iteration. The java.util.Arrays class provides a set of powerful utility methods for sorting, searching, copying, comparing, and manipulating arrays. Understanding these methods is crucial for writing clean, performant, and maintainable code.

In this tutorial, we will explore the most commonly used methods in the Arrays class such as sort(), binarySearch(), copyOf(), equals(), fill(), toString(), and mismatch() with detailed examples, performance considerations, and best practices.

What is java.util.Arrays?

java.util.Arrays is a utility class that provides static methods to manipulate arrays. It is part of the Java Collections Framework and is available since JDK 1.2. All methods are static, so they can be called without creating an instance.

import java.util.Arrays;

Key Methods in Arrays Class

1. sort()

Sorts the array into ascending order.

int[] numbers = {5, 2, 9, 1, 7};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers)); // [1, 2, 5, 7, 9]
  • Performance: Uses Dual-Pivot Quicksort for primitives (O(n log n)) and TimSort for objects.
  • When to use: When you need an in-place sorted array.

2. binarySearch()

Performs binary search on a sorted array.

int[] numbers = {1, 2, 5, 7, 9};
int index = Arrays.binarySearch(numbers, 7);
System.out.println(index); // Output: 3
  • Important: The array must be sorted before using binarySearch().
  • When to use: Fast lookups in sorted arrays.

3. copyOf() and copyOfRange()

Copies an array to a new array with the specified length.

int[] numbers = {1, 2, 3};
int[] copy = Arrays.copyOf(numbers, 5);
System.out.println(Arrays.toString(copy)); // [1, 2, 3, 0, 0]

4. equals()

Checks if two arrays are equal (element-wise).

int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(Arrays.equals(a, b)); // true

5. fill()

Fills the entire array with a given value.

int[] arr = new int[5];
Arrays.fill(arr, 7);
System.out.println(Arrays.toString(arr)); // [7, 7, 7, 7, 7]

6. toString()

Returns a string representation of an array.

int[] arr = {1, 2, 3};
System.out.println(Arrays.toString(arr)); // [1, 2, 3]

7. mismatch() (Java 9+)

Finds the first index where two arrays differ.

int[] a = {1, 2, 3};
int[] b = {1, 2, 4};
System.out.println(Arrays.mismatch(a, b)); // Output: 2

Performance and Memory Implications

  • sort() is O(n log n), efficient for most use cases.
  • binarySearch() is O(log n), but only works on sorted arrays.
  • copyOf() creates a new array, so avoid unnecessary copies in large datasets.

Common Mistakes & Anti-Patterns

  • Using binarySearch() on unsorted arrays.
  • Forgetting that copyOf() creates a new array (changes won’t reflect in the original).
  • Assuming Arrays.equals() compares nested arrays (for deep comparison use Arrays.deepEquals()).

Real-World Use Cases

  • Sorting leaderboard scores.
  • Searching in configuration arrays.
  • Initializing arrays with default values using fill().
  • Comparing cache states using equals().

Best Practices

  • Always sort before using binarySearch().
  • Use copyOf() when resizing arrays instead of manual loops.
  • Prefer Arrays.deepEquals() for multidimensional arrays.

Java Version Relevance

Method Added In
sort() Java 1.2
binarySearch() Java 1.2
copyOf() Java 1.6
mismatch() Java 9

Interview Questions

  1. Difference between Arrays.equals() and Arrays.deepEquals()?
  2. Time complexity of Arrays.sort()?
  3. Why must you sort an array before binarySearch()?

Conclusion

The Arrays utility class is a powerful set of tools for working with arrays in Java. By mastering methods like sort(), binarySearch(), and copyOf(), you can write more efficient and cleaner code.


FAQ

Q: Can I sort a part of an array?
A: Yes, use Arrays.sort(arr, fromIndex, toIndex).

Q: Is Arrays.sort() stable?
A: For objects, yes (TimSort). For primitives, stability is not guaranteed.