Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

The Arrays class

Overview of java.util.Arrays: sorting, searching, filling, copying, comparing, printing and streaming arrays, with the most useful methods in one place.

java.util.Arrays is the toolbox for arrays. Java arrays have almost no behavior of their own (just length and clone()), so printing, sorting, searching, comparing and copying all live here as static methods. Most come in overloaded versions for every primitive type plus Object[].

Remember that arrays do not override toString() or equals(). Printing an array directly gives something like [I@1b6d3586, and == compares references. Arrays fixes both.

Java
int[] a = {3, 1, 2};
Arrays.sort(a);
System.out.println(Arrays.toString(a));

Try it

Java The everyday methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Nested arrays and comparisons

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

All members are static. Each has overloads for int[], long[], double[], char[] and so on, plus Object[] or T[].

Method Description
toString(a), deepToString(a) Readable text for arrays and nested arrays.
sort(a), sort(a, from, to), sort(T[], Comparator) In-place sort.
parallelSort(a) Sort using the fork/join pool.
binarySearch(a, key) Search a sorted array.
copyOf(a, newLength), copyOfRange(a, from, to) New arrays.
fill(a, value), setAll(a, generator) Fill with a value or computed values.
equals(a, b), deepEquals(a, b), hashCode(a), deepHashCode(a) Content-based comparison.
compare(a, b), mismatch(a, b) Lexicographic comparison; index of first difference (Java 9+).
asList(T...) Fixed-size list view of an array.
stream(a) A Stream or IntStream over the elements.

Throws: NullPointerException for a null array (except in equals and hashCode where null is handled); ArrayIndexOutOfBoundsException for bad ranges; IllegalArgumentException when from > to.

Usage notes

  • Shallow vs deep: toString, equals and hashCode look one level deep. For arrays of arrays use the deep... versions.
  • Primitive sorts are not stable (dual-pivot quicksort); object sorts are stable (TimSort).
  • No boxing for primitives: Arrays.sort(int[]) cannot take a comparator. To sort descending, use Integer[] or sort and reverse.
  • Arrays.asList(int[]) gives a one-element list containing the array, because generics cannot hold primitives. Use Integer[].
  • Arrays are fixed size. "Resizing" means copyOf into a bigger array.
  • Copies are shallow: copying an object array copies references, not objects.
  • Thread safety: none; do not sort an array while another thread reads it.