Arrays class

The Arrays class in Java, located in the java.util package, provides utility methods for working with arrays. It offers various static methods to perform operations such as sorting, searching, comparing, and filling arrays. Here’s an overview of the Arrays class, its syntax, and examples demonstrating its usage.

Arrays class Methods 

Method Signature Description
public static <T> void sort(T[] a) Sorts the specified array into ascending order (natural order).
public static <T> void sort(T[] a, Comparator<? super T> c) Sorts the array using the specified comparator.
public static void sort(int[] a) Sorts the array of primitive int values in ascending order.
public static int binarySearch(int[] a, int key) Searches for key in a sorted int[] using binary search.
public static <T> int binarySearch(T[] a, T key) Searches for key in a sorted object array using binary search.
public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) Binary search with custom comparator.
public static <T> boolean equals(T[] a, T[] a2) Returns true if both arrays are equal in length and elements.
public static boolean equals(int[] a, int[] a2) Compares two int arrays for equality.
public static int compare(int[] a, int[] b) Compares two int arrays lexicographically.
public static <T> int compare(T[] a, T[] b, Comparator<? super T> c) Lexicographically compares arrays using comparator.
public static String toString(int[] a) Returns a string representation of the int[].
public static <T> String toString(T[] a) Returns a string representation of the object array.
public static int[] copyOf(int[] original, int newLength) Copies the original array to a new array with specified length.
public static <T> T[] copyOf(T[] original, int newLength) Copies an object array to a new array of given length.
public static <T> T[] copyOfRange(T[] original, int from, int to) Copies a range from original array.
public static boolean deepEquals(Object[] a1, Object[] a2) Compares nested arrays deeply.
public static int deepHashCode(Object[] a) Returns deep hash code of nested arrays.
public static String deepToString(Object[] a) Returns deep string representation of nested arrays.
public static void fill(int[] a, int val) Assigns the specified value to each element of the int[].
public static <T> void fill(T[] a, T val) Fills object array with specified value.
public static void parallelSort(int[] a) Sorts array using parallel sorting (faster for large arrays).
public static <T> void parallelSort(T[] a, Comparator<? super T> cmp) Parallel sort using comparator.
public static Spliterator.OfInt spliterator(int[] array) Returns a Spliterator for a primitive int array.
public static <T> List<T> asList(T... a) Converts array to a fixed-size list backed by the array.
public static <T> Stream<T> stream(T[] array) Returns a sequential stream from the array.
public static IntStream stream(int[] array) Returns an IntStream from int[].

The java.util.Arrays class is a powerful utility provided by Java to simplify array operations. It offers a wide range of static methods that enhance the functionality and convenience of working with arrays — from sorting, searching, and copying, to filling, comparing, and streaming. These methods support both primitive and object arrays, ensuring versatility across various types of applications.

Key benefits include:

  • Simplified sorting and searching (e.g., sort(), binarySearch()).

  • Easy conversions and representations (e.g., toString(), asList()).

  • Safe and efficient copying (e.g., copyOf(), copyOfRange()).

  • Functional programming support via Streams and Spliterators.

  • Enhanced performance with parallelSort for large datasets.

Scroll to Top