The Arrays utility class in Java
sort, toString, deepToString, fill, equals, binarySearch, asList, stream.
The toolbox
java.util.Arrays is a class full of static helper methods. Arrays.toString(a) gives readable text like [1, 2, 3]. **Arrays.sort(a) sorts the array in place, ascending, and returns void**: it changes the array you pass and doesn't make a new one.
int[] a = {3, 1, 2};
Arrays.sort(a); // a itself changes
System.out.println(Arrays.toString(a));
// [1, 2, 3]Sorting in place
b was never sorted... or was it? What prints?
int[] a = {9, 2, 7};
int[] b = a;
Arrays.sort(a);
System.out.println(Arrays.toString(b));[9, 2, 7][2, 7, 9][9, 7, 2]
Show the answer
b and a are the same array. Arrays.sort rearranges that one array in place, so b shows it sorted. Need the original order too? Copy first.
fill, stream, deepToString
**Arrays.fill(a, v)** sets every element to v. **Arrays.stream(a)** turns an array into a stream, so .sum(), .max() and friends work. For nested arrays use **Arrays.deepToString**: plain toString would print each row as gibberish.
int[] b = new int[3];
Arrays.fill(b, 7); // [7, 7, 7]
Arrays.stream(b).sum(); // 21
int[][] g = {{1}, {2, 3}};
Arrays.deepToString(g); // [[1], [2, 3]]Your turn
What prints?
int[][] g = {{1, 2}, {3}};
System.out.println(Arrays.deepToString(g));
int[] f = new int[3];
Arrays.fill(f, 4);
System.out.println(Arrays.stream(f).sum());[[1, 2], [3]] 12[[1, 2], [3]] 4[1, 2, 3] 12
Show the answer
deepToString opens up the nested rows. fill makes f = [4, 4, 4], and the stream's sum is 12.
binarySearch needs order
**Arrays.binarySearch(a, key) finds a value fast by repeatedly halving the search range, and returns its index. It requires a sorted array: on an unsorted one the result is undefined** (garbage, not an error).
int[] a = {5, 3, 9, 1};
Arrays.sort(a); // [1, 3, 5, 9]
Arrays.binarySearch(a, 5); // 2asList is a window
**Arrays.asList(arr) returns a fixed-size List backed by the array**. set writes straight through to the array. But add or remove would change the size, so they throw UnsupportedOperationException.
String[] arr = {"a", "b"};
List<String> list = Arrays.asList(arr);
list.set(0, "z"); // arr[0] is now "z"
list.add("c"); // throws!Through the window
What prints?
String[] arr = {"x", "y"};
List<String> list = Arrays.asList(arr);
list.set(1, "q");
System.out.println(arr[1]);yqx
Show the answer
The list is a view of the array, not a copy. list.set(1, "q") writes into arr[1], so the array shows q.
In real projects
A famous production surprise: someone wraps an array with Arrays.asList, passes it on, and code far away calls add, crashing with UnsupportedOperationException. When you need a growable list, use new ArrayList<>(Arrays.asList(arr)).
Key takeaways
- Arrays.sort sorts in place, ascending
- toString for 1-D arrays, deepToString for nested ones
- binarySearch needs a sorted array, or the result is undefined
- Arrays.asList returns a fixed-size List backed by the array
Arrays.sort uses Dual-Pivot Quicksort for primitive arrays (since Java 7) and TimSort, a stable merge-sort hybrid, for arrays of objects.
Practice questions
What does this print?
int[] a = {5, 3, 9, 1};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
System.out.println(Arrays.binarySearch(a, 5));- [1, 3, 5, 9] 0
- [5, 3, 9, 1] 0
- [1, 3, 5, 9] 2
- [1, 3, 5, 9] 3
Check your answer
[1, 3, 5, 9] 2. After sorting, the array is [1, 3, 5, 9], and binarySearch finds 5 at index 2.
What does this print?
int[] a = {1, 2, 3, 4};
System.out.println(Arrays.stream(a).sum());
int[] b = new int[3];
Arrays.fill(b, 7);
System.out.println(Arrays.toString(b));- 10 [7, 7, 7]
- 4 [7, 7, 7]
- 10 [7, 0, 0]
- 10 [0, 0, 0]
Check your answer
10 [7, 7, 7]. Arrays.stream turns the array into an IntStream whose sum is 10. fill sets every slot of b to 7.