Java arrays are powerful, but many everyday operations become much easier when you use the java.util.Arrays utility class. It provides ready-to-use methods for searching, sorting, copying, comparing, filling, and displaying array contents.
Why Use the Arrays Utility Class?
Without utility methods, you would repeatedly write loops for common array operations. The Arrays class provides tested and readable methods that reduce unnecessary code and make your intention clearer.
import java.util.Arrays;
After importing the class, its static methods can be called using Arrays.methodName().
Arrays.toString()
Printing an array directly does not display its elements in a useful format.
int[] numbers = {10, 20, 30};
System.out.println(numbers);
Instead of the actual values, Java prints a representation of the array object. Use Arrays.toString() to display a one-dimensional array clearly.
int[] numbers = {10, 20, 30};
System.out.println(Arrays.toString(numbers));
The output is [10, 20, 30]. This is especially useful while debugging and inspecting program output.
Arrays.deepToString()
For multidimensional arrays, use Arrays.deepToString() when you want to display nested array contents.
int[][] numbers = {
{10, 20},
{30, 40}
};
System.out.println(Arrays.deepToString(numbers));
The output represents the nested structure as [[10, 20], [30, 40]].
Arrays.sort()
The Arrays.sort() method sorts array elements into ascending order for commonly used primitive and reference types.
int[] numbers = {50, 10, 40, 20, 30};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
After sorting, the array becomes [10, 20, 30, 40, 50]. Notice that Arrays.sort() changes the original array rather than returning a new sorted array.
Important: If the original ordering is needed later, create a copy before sorting the array.
Sorting a Range
You can sort only part of an array by providing a starting index and an ending index.
int[] numbers = {50, 40, 30, 20, 10};
Arrays.sort(numbers, 1, 4);
System.out.println(Arrays.toString(numbers));
Only indexes 1 through 3 are sorted. As with many Java range-based methods, the ending index is excluded.
Arrays.binarySearch()
The Arrays.binarySearch() method searches for a value using binary search.
int[] numbers = {10, 20, 30, 40, 50}; int index = Arrays.binarySearch(numbers, 30); System.out.println(index);
The method returns the index of the searched value when it is found. Binary search is efficient, but there is one important requirement: the array must already be sorted according to the ordering expected by the search.
Remember: Do not use Arrays.binarySearch() on an unsorted array and expect a meaningful result.
Searching for a Missing Element
If the searched value is not present, binarySearch() returns a negative value that encodes the insertion position. In most application code, you only need to remember that a negative result means the value was not found.
int[] numbers = {10, 20, 30, 40, 50}; int index = Arrays.binarySearch(numbers, 35); if (index >= 0) { System.out.println("Found at index " + index); } else { System.out.println("Value not found"); }
Arrays.equals()
The Arrays.equals() method compares the contents of two one-dimensional arrays.
int[] first = {10, 20, 30}; int[] second = {10, 20, 30}; System.out.println(Arrays.equals(first, second));
The result is true because both arrays contain the same values in the same order.
Arrays.deepEquals()
For nested arrays, use Arrays.deepEquals().
int[][] first = { {10, 20}, {30, 40} }; int[][] second = { {10, 20}, {30, 40} }; System.out.println(Arrays.deepEquals(first, second));
This method recursively compares nested arrays rather than comparing only the references of the inner arrays.
Arrays.fill()
The Arrays.fill() method assigns the same value to every element of an array.
int[] numbers = new int[5]; Arrays.fill(numbers, 100); System.out.println(Arrays.toString(numbers));
Every position now contains 100.
Filling a Specific Range
You can also fill only part of an array.
int[] numbers = {10, 20, 30, 40, 50};
Arrays.fill(numbers, 1, 4, 99);
System.out.println(Arrays.toString(numbers));
Indexes 1, 2, and 3 become 99, while indexes 0 and 4 remain unchanged.
Arrays.copyOf()
The Arrays.copyOf() method creates a new array with the requested length.
int[] original = {10, 20, 30}; int[] copy = Arrays.copyOf(original, 5); System.out.println(Arrays.toString(copy));
The copied array contains the original values followed by default values for the additional positions.
Arrays.copyOfRange()
When you need a specific portion of an array, Arrays.copyOfRange() is convenient.
int[] numbers = {10, 20, 30, 40, 50}; int[] part = Arrays.copyOfRange(numbers, 1, 4); System.out.println(Arrays.toString(part));
The resulting array contains 20, 30, and 40. The starting index is included, while the ending index is excluded.
Arrays.compare()
Java also provides Arrays.compare() for lexicographical comparison. Instead of returning only true or false, it returns a negative value, zero, or a positive value depending on the ordering of the arrays.
int[] first = {10, 20, 30}; int[] second = {10, 20, 40}; int result = Arrays.compare(first, second); System.out.println(result);
Because the first differing values are 30 and 40, the first array is considered smaller than the second. This method is useful when array ordering matters rather than simple equality.
Arrays.mismatch()
The Arrays.mismatch() method finds the first index at which two arrays differ.
int[] first = {10, 20, 30, 40}; int[] second = {10, 20, 35, 40}; int index = Arrays.mismatch(first, second); System.out.println(index);
The result is 2 because index 2 is the first position where the arrays contain different values.
Practical Example: Processing Product Prices
Imagine an application receiving product prices from a data source. You may need to sort them, search for a particular price, display them, and compare them with another dataset.
int[] prices = {499, 199, 999, 299, 799}; Arrays.sort(prices); System.out.println("Sorted prices: " + Arrays.toString(prices)); int index = Arrays.binarySearch(prices, 499); System.out.println("499 found at index: " + index);
The example demonstrates why utility methods are valuable: several common operations can be expressed clearly without writing separate loops for each task.
Common Beginner Mistakes
- Printing an array directly instead of using Arrays.toString().
- Using binarySearch() before sorting the array.
- Forgetting that sort() changes the original array.
- Using Arrays.equals() when a deep comparison is required.
- Forgetting that range-ending indexes are generally exclusive.
Interview Insight
Interviewers often ask which utility method should be used for a particular task. A good rule is simple: use toString() for displaying a one-dimensional array, deepToString() for nested arrays, sort() for ordering, binarySearch() for searching a sorted array, equals() for one-dimensional content comparison, and deepEquals() for nested content comparison.
| Method | Purpose | Changes Original? |
|---|---|---|
| toString() | Convert one-dimensional array contents to a readable string | No |
| deepToString() | Display nested array contents | No |
| sort() | Sort array elements | Yes |
| binarySearch() | Search a sorted array | No |
| equals() | Compare one-dimensional contents | No |
| deepEquals() | Compare nested array contents | No |
| fill() | Assign a value to elements | Yes |
| copyOf() | Create an array copy with a chosen length | No |
| copyOfRange() | Create a copy from a selected range | No |
| compare() | Lexicographically compare arrays | No |
| mismatch() | Find the first differing index | No |
The Arrays utility class turns many repetitive array tasks into concise, readable operations. Learning these methods is an important step toward writing professional Java code because you spend less time reinventing common logic and more time expressing what your program actually needs to accomplish.
