🧱 Arrays · Beginner

The Arrays utility class in Java

sort, toString, deepToString, fill, equals, binarySearch, asList, stream.

🧩 The mysteryPrinting an array shows something like [I@1b6d3586. Sorting it takes 20 lines of loops. Or one line each, if you know where Java hid the toolbox.

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]
🔮 Predict it

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));
  1. [9, 2, 7]
  2. [2, 7, 9]
  3. [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]]
🔮 Predict it

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. [[1, 2], [3]] 12
  2. [[1, 2], [3]] 4
  3. [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); // 2
⚠️ The trap

asList 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!
🔮 Predict it

Through the window

What prints?

String[] arr = {"x", "y"};
List<String> list = Arrays.asList(arr);
list.set(1, "q");
System.out.println(arr[1]);
  1. y
  2. q
  3. x
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 the real world

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

  1. Arrays.sort sorts in place, ascending
  2. toString for 1-D arrays, deepToString for nested ones
  3. binarySearch needs a sorted array, or the result is undefined
  4. Arrays.asList returns a fixed-size List backed by the array
🤯 Did you know?

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. [1, 3, 5, 9] 0
  2. [5, 3, 9, 1] 0
  3. [1, 3, 5, 9] 2
  4. [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));
  1. 10 [7, 7, 7]
  2. 4 [7, 7, 7]
  3. 10 [7, 0, 0]
  4. 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.

Next: two arrays with identical contents, and Java says they're not equal. Twice.