Arrays Class

Utility methods from java.util.Arrays

Interview Relevant: Know these methods to save time in coding tests
6 min read

The java.util.Arrays Class

The Arrays class provides a collection of static utility methods for working with arrays efficiently.

Key Methods Overview

Method Purpose
sort() Sort array elements
binarySearch() Search sorted array
equals() Compare two arrays
fill() Fill array with value
copyOf() Copy array
toString() String representation

💡 Import Required: import java.util.Arrays;

Code Examples

toString() and sort() methods

java
1import java.util.Arrays;
2
3// toString() - Print array contents
4int[] arr = {5, 2, 8, 1, 9};
5System.out.println(arr);                    // [I@hashcode (useless!)
6System.out.println(Arrays.toString(arr));   // [5, 2, 8, 1, 9]
7
8// For 2D arrays, use deepToString()
9int[][] matrix = {{1, 2}, {3, 4}};
10System.out.println(Arrays.deepToString(matrix));  // [[1, 2], [3, 4]]
11
12// sort() - Sort array
13int[] nums = {5, 2, 8, 1, 9};
14Arrays.sort(nums);
15System.out.println(Arrays.toString(nums));  // [1, 2, 5, 8, 9]
16
17// Sort range only
18int[] partial = {5, 2, 8, 1, 9};
19Arrays.sort(partial, 1, 4);  // Sort indices 1 to 3
20System.out.println(Arrays.toString(partial));  // [5, 1, 2, 8, 9]

binarySearch() and equals() methods

java
1// binarySearch() - Search in SORTED array
2int[] sorted = {1, 2, 5, 8, 9};
3int index = Arrays.binarySearch(sorted, 5);
4System.out.println("Index of 5: " + index);  // 2
5
6// Not found returns -(insertion point) - 1
7int notFound = Arrays.binarySearch(sorted, 6);
8System.out.println("Index of 6: " + notFound);  // -4
9
10// equals() - Compare arrays
11int[] a = {1, 2, 3};
12int[] b = {1, 2, 3};
13int[] c = {1, 2, 4};
14
15System.out.println(a == b);                    // false (different objects)
16System.out.println(Arrays.equals(a, b));       // true (same content)
17System.out.println(Arrays.equals(a, c));       // false
18
19// For 2D arrays, use deepEquals()
20int[][] m1 = {{1, 2}, {3, 4}};
21int[][] m2 = {{1, 2}, {3, 4}};
22System.out.println(Arrays.deepEquals(m1, m2));  // true

fill(), copyOf(), and copyOfRange() methods

java
1// fill() - Fill array with value
2int[] zeros = new int[5];
3Arrays.fill(zeros, 0);
4System.out.println(Arrays.toString(zeros));  // [0, 0, 0, 0, 0]
5
6// Fill range
7int[] arr = new int[5];
8Arrays.fill(arr, 1, 4, 9);  // Fill indices 1-3 with 9
9System.out.println(Arrays.toString(arr));  // [0, 9, 9, 9, 0]
10
11// copyOf() - Copy array
12int[] original = {1, 2, 3, 4, 5};
13int[] copy = Arrays.copyOf(original, original.length);
14int[] larger = Arrays.copyOf(original, 10);  // Extends with zeros
15int[] smaller = Arrays.copyOf(original, 3);  // Truncates
16
17System.out.println(Arrays.toString(larger));   // [1, 2, 3, 4, 5, 0, 0, 0, 0, 0]
18System.out.println(Arrays.toString(smaller));  // [1, 2, 3]
19
20// copyOfRange() - Copy range
21int[] range = Arrays.copyOfRange(original, 1, 4);
22System.out.println(Arrays.toString(range));  // [2, 3, 4]

asList(), stream(), and parallelSort() methods

java
1// asList() - Convert array to List
2String[] names = {"Alice", "Bob", "Charlie"};
3List<String> list = Arrays.asList(names);
4System.out.println(list);  // [Alice, Bob, Charlie]
5
6// Warning: List is backed by array!
7list.set(0, "Anna");
8System.out.println(names[0]);  // Anna (array also changed!)
9
10// Cannot add/remove elements
11// list.add("Dave");  // UnsupportedOperationException!
12
13// For modifiable list:
14List<String> modifiable = new ArrayList<>(Arrays.asList(names));
15
16// stream() - Create stream from array (Java 8+)
17int[] numbers = {1, 2, 3, 4, 5};
18int sum = Arrays.stream(numbers).sum();
19int max = Arrays.stream(numbers).max().orElse(0);
20
21System.out.println("Sum: " + sum);  // 15
22System.out.println("Max: " + max);  // 5
23
24// parallelSort() - Faster for large arrays
25int[] large = new int[1000000];
26Arrays.parallelSort(large);  // Uses multiple threads

Use Cases

  • Quick array printing for debugging
  • Efficient array sorting
  • Binary search for fast lookups
  • Array comparison
  • Array initialization with default values
  • Converting between arrays and collections

Common Mistakes to Avoid

  • Using binarySearch() on unsorted array
  • Using == instead of Arrays.equals()
  • Modifying List from asList() expecting it to grow
  • Using toString() on 2D array (use deepToString)
  • Not importing java.util.Arrays
  • Forgetting that copyOf creates shallow copy for objects