Codectionary / Developer documentation / Java

Arrays Utility Class

java.util.Arrays is a utility class providing static helper methods specifically for working with arrays - sorting, searching, filling, comparing, copying, and converting to a readable String or a List. Since arrays don't have built-in methods of their own the way ArrayList does, this utility class fills that gap for common array tasks.

Syntax

Arrays.methodName(array);

Examples

Sorting and Printing Arrays

Sorting an array and converting it to a readable string.

import java.util.Arrays;

int[] numbers = {5, 2, 8, 1, 9};

Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));  // [1, 2, 5, 8, 9]

int[][] matrix = {{1, 2}, {3, 4}};
System.out.println(Arrays.deepToString(matrix));  // [[1, 2], [3, 4]]

Searching and Filling

Binary search on a sorted array, and filling an array with a value.

import java.util.Arrays;

int[] sorted = {1, 3, 5, 7, 9};
int index = Arrays.binarySearch(sorted, 7);
System.out.println(index);  // 3

int[] scores = new int[5];
Arrays.fill(scores, 100);
System.out.println(Arrays.toString(scores));  // [100, 100, 100, 100, 100]

Copying Arrays

Creating independent copies, whole or partial, of an array.

import java.util.Arrays;

int[] original = {1, 2, 3, 4, 5};

int[] fullCopy = Arrays.copyOf(original, original.length);
int[] extended = Arrays.copyOf(original, 8);       // pads with 0s
int[] partial = Arrays.copyOfRange(original, 1, 4); // indices 1 to 3

System.out.println(Arrays.toString(extended));  // [1, 2, 3, 4, 5, 0, 0, 0]
System.out.println(Arrays.toString(partial));    // [2, 3, 4]

Converting Between Array and List

Arrays.asList() provides a quick bridge between an array and the List interface.

import java.util.Arrays;
import java.util.List;

String[] namesArray = {"Fola", "Zain", "Jamal"};
List<String> namesList = Arrays.asList(namesArray);

System.out.println(namesList);  // [Fola, Zain, Jamal]

// Note: Arrays.asList() returns a FIXED-SIZE list backed by the array
// namesList.add("New");  // would throw UnsupportedOperationException

Best practices

  • Use Arrays.toString() or Arrays.deepToString() (for multi-dimensional arrays) to print array contents - printing an array directly shows only its memory reference
  • Remember Arrays.asList() returns a fixed-size list backed by the original array - wrap it in new ArrayList<>(...) if you need a fully resizable list
  • Use Arrays.sort() before Arrays.binarySearch() - binary search requires the array to already be sorted to work correctly
  • Use Arrays.equals() to compare array contents - == and .equals() on arrays only compare references, not element values

At a glance

Purpose
General-purpose application development
File extension
.java
Runs in
Java Virtual Machine
Usually used with
JDK and Java libraries

Specifications & further reading

Related Java documentation

ArrayList
ArrayList is a resizable array implementation of the List interface, part of java.util. Unlike a plain array, an ArrayList automatically grows as elements are added, and it provides a rich set of methods for adding, removing, searching, and iterating. Since generics require object types, an ArrayList of primitives (like int) actually stores their wrapper class (Integer) via autoboxing.
LinkedList
LinkedList is a doubly-linked list implementation of both the List and Deque interfaces. Unlike ArrayList, it stores elements as individual nodes linked to their neighbors, which makes inserting and removing elements at the beginning or middle much faster, at the cost of slower random access by index. Because it implements Deque, LinkedList can also be used directly as a stack or queue.
HashMap
HashMap stores data as key-value pairs, offering constant-time (average case) lookup, insertion, and deletion by key, backed by a hash table. Keys must be unique - adding a value with an existing key overwrites the previous value. HashMap does not guarantee any particular ordering of its entries, unlike LinkedHashMap (which preserves insertion order) or TreeMap (which keeps keys sorted).
HashSet
HashSet is a collection that stores unique elements with no guaranteed ordering, backed by a HashMap internally. Adding a duplicate element has no effect, since HashSet automatically enforces uniqueness. It provides constant-time (average case) performance for adding, removing, and checking membership, making it ideal for deduplication and fast lookups.