W3docs

Java Arrays Utility Class

Use the java.util.Arrays class to sort, search, fill, compare, and convert arrays in Java.

java.util.Arrays is the standard library's collection of array helpers. It's a final class with only static methods — you never instantiate it, you just call its methods directly: Arrays.sort(...), Arrays.toString(...), and so on. Once you know what's in it, the array-handling code you write gets a lot shorter.

This chapter is a guided tour. Sorting and copying have their own chapters; here we focus on the rest.

Importing

import java.util.Arrays;

Almost every example below assumes this import.

toString and deepToString

Turn an array into a readable string. toString handles one-dimensional arrays:

int[] data = {3, 1, 4};
String s = Arrays.toString(data);   // "[3, 1, 4]"

deepToString recursively formats nested arrays:

int[][] grid = {{1, 2}, {3, 4}};
String s = Arrays.deepToString(grid);   // "[[1, 2], [3, 4]]"

Both work for primitive and object element types.

equals and deepEquals

== compares array references. To compare contents:

int[] a = {1, 2, 3};
int[] b = {1, 2, 3};
boolean same = Arrays.equals(a, b);   // true

For nested arrays use deepEquals:

int[][] g1 = {{1, 2}, {3, 4}};
int[][] g2 = {{1, 2}, {3, 4}};
boolean same = Arrays.deepEquals(g1, g2);   // true

Both consider null == null to be true.

hashCode and deepHashCode

Content-based hashes, useful when a class wraps an array and needs equals/hashCode:

int[] data = {1, 2, 3};
int h = Arrays.hashCode(data);

If you override equals to use Arrays.equals, you must override hashCode to use Arrays.hashCode to keep the contract.

fill

Set every element to a value, or set a range:

int[] data = new int[5];
Arrays.fill(data, 7);              // {7, 7, 7, 7, 7}
Arrays.fill(data, 1, 4, 0);        // zero indexes 1..3

sort and parallelSort

In-place sort, ascending:

int[] data = {3, 1, 4, 1, 5};
Arrays.sort(data);                 // {1, 1, 3, 4, 5}

For very large arrays you can use parallelSort to spread the work across multiple cores. The full sorting story — primitives, objects, custom orders — is in the Sorting arrays chapter.

binarySearch

Find an element in a sorted array in O(log n):

int[] sorted = {1, 3, 5, 7, 9};
int idx = Arrays.binarySearch(sorted, 5);   // 2
int miss = Arrays.binarySearch(sorted, 6);  // negative — encodes insertion point

If the element is missing, the return value is -(insertionPoint) - 1. So miss == -4 means 6 would belong at index 3. If the input isn't sorted, the result is undefined — sort first.

copyOf and copyOfRange

Return a new array, copying values out of an existing one:

int[] data = {1, 2, 3, 4, 5};
int[] all   = Arrays.copyOf(data, data.length);    // exact copy
int[] grown = Arrays.copyOf(data, 8);              // padded with zeros
int[] slice = Arrays.copyOfRange(data, 1, 4);      // {2, 3, 4}

These are covered in detail in Copying arrays.

asList

Wrap an array of references as a fixed-size List:

String[] arr = {"a", "b", "c"};
List<String> list = Arrays.asList(arr);

The list is backed by the array — list.set(0, "z") also changes arr[0]. Size is fixed, so add/remove throw. With primitive arrays it doesn't do what you'd expect: Arrays.asList(new int[]{1, 2, 3}) produces a List<int[]> of length one. For primitives, box with streams first.

stream

Get a stream from any numeric or object array:

int[] nums = {3, 1, 4, 1, 5};
int sum = Arrays.stream(nums).sum();
double avg = Arrays.stream(nums).average().orElse(0);

For object arrays, Arrays.stream(arr) returns a Stream<T>. There are also range-limited forms — Arrays.stream(arr, from, to).

setAll and parallelSetAll

Fill an array using a function of the index:

int[] squares = new int[6];
Arrays.setAll(squares, i -> i * i);
// {0, 1, 4, 9, 16, 25}

Use setAll when you want a derived sequence and a for loop would just be noise.

compare and mismatch (Java 9+)

Arrays.compare(a, b) returns negative, zero, or positive — lexicographic order over the elements:

int[] a = {1, 2, 3};
int[] b = {1, 2, 4};
int cmp = Arrays.compare(a, b);   // negative — a is smaller

Arrays.mismatch(a, b) returns the index of the first differing element, or -1 if they're equal:

int diff = Arrays.mismatch(a, b);   // 2

These are useful when you need order or "where did they diverge" without writing the loop by hand.

A worked example

java— editable, runs on the server

What's next

We've used Arrays.sort only at a glance. The next chapter, Sorting arrays, digs into how sorting works for primitives versus objects, ascending versus descending, and how to sort by your own criteria with a Comparator.

Practice

Practice

Why is Arrays.binarySearch faster than a linear loop?