1. Introduction
Arrays in Java are a powerful tool, but the basic syntax quickly runs into boilerplate. The java.util.Arrays class comes to the rescue — a set of static methods for working with arrays: sorting, copying, comparing, formatted output, and more. No need to create instances — all methods are static and called via the class name.
To use the methods, add an import at the top of the file:
import java.util.Arrays;
If you forget the import, the compiler will complain that Arrays is “not found” — this isn’t a bug; it’s the expected behaviour without the import.
2. Sorting an array: Arrays.sort
How Arrays.sort works
Syntax
Arrays.sort(array);
Arrays.sort(array, startIndex, endIndex);
The method sorts in place — it mutates the original array and does not return a new one. Want to keep the original order? Copy the array first.
Example: sorting an int array
int[] scores = {5, 2, 9, 1, 7};
Arrays.sort(scores); // Sort ascending
System.out.println("Sorted array: " + Arrays.toString(scores));
// Prints: [1, 2, 5, 7, 9]
Example: sorting a String array
String[] names = {"Alice", "Bob", "Charlie", "David"};
Arrays.sort(names);
System.out.println(Arrays.toString(names));
// [Charlie, David, Bob, Alice]
Important: strings are sorted lexicographically (alphabetically), numbers in ascending order.
Sorting part of an array
int[] arr = {7, 5, 3, 1, 9, 8};
Arrays.sort(arr, 1, 4); // sorts elements from index 1 to 3 inclusive (4 is excluded)
System.out.println(Arrays.toString(arr));
// [7, 1, 3, 5, 9, 8]
Details
- In-place sorting: the original array is modified.
- Numbers — ascending; strings — alphabetical.
- For complex objects, use the overloads with a comparator.
3. Filling an array: Arrays.fill
Syntax
Arrays.fill(array, value);
Arrays.fill(array, startIndex, endIndex, value);
The method fills all elements or the specified part of an array with the same value.
int[] marks = new int[5];
Arrays.fill(marks, 3); // All elements are now 3
System.out.println(Arrays.toString(marks)); // [3, 3, 3, 3, 3]
Filling part of an array
int[] arr = new int[10];
Arrays.fill(arr, 2, 5, 7); // Will fill elements with indices 2 through 4 inclusive (5 is excluded)
System.out.println(Arrays.toString(arr));
// [0, 0, 7, 7, 7, 0, 0, 0, 0, 0]
Filling a String array
String[] guests = new String[4];
Arrays.fill(guests, "Empty");
System.out.println(Arrays.toString(guests)); // [Empty, Empty, Empty, Empty]
4. Copying an array: Arrays.copyOf and Arrays.copyOfRange
Arrays.copyOf
Syntax
Arrays.copyOf(originalArray, newLength);
Creates a new array and copies elements from the original. If the new length is greater than the original, the “tail” is filled with default values (0 for int, null for reference types).
int[] original = {1, 2, 3};
int[] copy = Arrays.copyOf(original, 5);
System.out.println(Arrays.toString(copy)); // [1, 2, 3, 0, 0]
Shortening an array
int[] shortCopy = Arrays.copyOf(original, 2);
System.out.println(Arrays.toString(shortCopy)); // [1, 2]
Arrays.copyOfRange
Syntax
Arrays.copyOfRange(originalArray, startIndex, endIndex);
int[] arr = {10, 20, 30, 40, 50};
int[] mid = Arrays.copyOfRange(arr, 1, 4); // Will copy elements with indices 1 through 3
System.out.println(Arrays.toString(mid)); // [20, 30, 40]
5. Comparing arrays: Arrays.equals and Arrays.deepEquals
In Java you can’t compare arrays with == — that compares references, not contents:
int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
System.out.println(a == b); // false!
How to compare contents?
Arrays.equals(array1, array2);
Arrays.deepEquals(matrix1, matrix2);
System.out.println(Arrays.equals(a, b)); // true
Use Arrays.equals for one-dimensional arrays, and Arrays.deepEquals for two-dimensional and deeper.
Example: comparing two-dimensional arrays
int[][] matrix1 = {{1, 2}, {3, 4}};
int[][] matrix2 = {{1, 2}, {3, 4}};
System.out.println(Arrays.equals(matrix1, matrix2)); // false!
System.out.println(Arrays.deepEquals(matrix1, matrix2)); // true
6. Pretty-printing an array: Arrays.toString and Arrays.deepToString
Printing an array directly produces an unreadable string:
int[] arr = {1, 2, 3};
System.out.println(arr); // [I@7a81197d
Syntax
Arrays.toString(array);
Arrays.deepToString(matrix);
System.out.println(Arrays.toString(arr)); // [1, 2, 3]
int[][] matrix = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepToString(matrix)); // [[1, 2], [3, 4]]
7. Common mistakes when working with the Arrays class
Error #1: trying to compare arrays with ==
Arrays are objects, and == compares only references. Even if the values are the same, the result will be false. Use Arrays.equals (for one-dimensional) or Arrays.deepEquals (for multidimensional).
Error #2: forgetting that sorting mutates the array “in place”
If you need the original order, first make a copy with Arrays.copyOf, and only then sort the copy. Otherwise the original will lose its order.
Error #3: misunderstanding the bounds in Arrays.fill and Arrays.copyOfRange
These methods use a half-open range: the right bound is exclusive. For example, Arrays.fill(arr, 2, 5, 9) will fill indices 2, 3, 4, and will not touch the element at index 5.
Error #4: trying to print an array directly
Calling System.out.println(arr) prints a technical representation. Use Arrays.toString for one-dimensional and Arrays.deepToString for multidimensional arrays.
Error #5: comparing multidimensional arrays with Arrays.equals
The method compares only the top level (references to inner arrays). Use Arrays.deepEquals to compare contents.
GO TO FULL VERSION