Arrays Class in Java

Last Updated : 26 Nov, 2026

The Arrays class in Java is a utility class available in the java.util package. It provides static methods for performing operations such as sorting, searching, comparing, and manipulating arrays.

  • Supports operations on both primitive and object arrays.
  • Cannot be instantiated because it has a private constructor.
  • Provides methods for converting arrays into lists, streams, and string representations.

Example: Using the Arrays class to sort and display an array.

Java
import java.util.Arrays;

public class Geeks{
    public static void main(String[] args){

        int[] arr = {10, 20, 15, 22, 35};

        Arrays.sort(arr);

        System.out.println(Arrays.toString(arr));
    }
}

Output
[10, 15, 20, 22, 35]

Explanation: The sort() method arranges the array elements in ascending order, while toString() returns their string representation.

Class Declaration

Arrays is a final utility class that implicitly extends the Object class.

public final class Arrays

Its methods can be accessed directly using the class name without creating an object.

Syntax:

Arrays.methodName(arguments);

To use the Arrays class, import it from the java.util package.

import java.util.Arrays;

Commonly Used Methods of Arrays Class

1. asList() Method

The asList() method returns a fixed-size list backed by the specified array. Changes made to existing elements through the list are reflected in the original array and vice versa.

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

public class Geeks{
    public static void main(String[] args){

        Integer[] arr = {10, 20, 15, 22, 35};

        List<Integer> list = Arrays.asList(arr);

        System.out.println("Array as List: " + list);
    }
}

Output
Array as List: [10, 20, 15, 22, 35]

Explanation: The asList() method returns a list backed by the Integer array. Elements can be updated using set(), but operations that change the list size, such as add() and remove(), are not supported.

Note: When a primitive array such as int[] is passed to asList(), the entire array is treated as a single list element. Use wrapper arrays such as Integer[] to obtain a list of individual elements.

2. binarySearch() Method

The binarySearch() method searches for a specified element in a sorted array using the binary search algorithm.

Java
import java.util.Arrays;

public class Geeks{
    public static void main(String[] args){

        int[] arr = {10, 20, 15, 22, 35};

        Arrays.sort(arr);

        int key = 22;

        int index = Arrays.binarySearch(arr, key);

        System.out.println(key + " found at index: " + index);
    }
}

Output
22 found at index: 3

Explanation:

  • sort() arranges the elements in ascending order.
  • binarySearch() searches for the specified key.
  • If the element is found, its index is returned.
  • If the element is not found, the method returns -(insertion point) - 1.

The array must be sorted before performing binary search; otherwise, the result is undefined.

3. binarySearch(array, fromIndex, toIndex, key) Method

Method searches for an element within a specified range of a sorted array. The starting index is inclusive, while the ending index is exclusive.

Java
import java.util.Arrays;

public class Geeks{
    public static void main(String[] args){

        int[] arr = {10, 20, 15, 22, 35};

        Arrays.sort(arr);

        int key = 22;

        int result = Arrays.binarySearch(arr, 1, 3, key);

        System.out.println("Search result: " + result);
    }
}

Output
Search result: -4

Explanation: After sorting, the array becomes [10, 15, 20, 22, 35]. The search covers indices 1 and 2, containing 15 and 20. Since 22 is not present within this range, the method returns -4, calculated using the insertion point of 3.

4. compare() Method

The compare() method, introduced in Java 9, compares two arrays lexicographically by examining their corresponding elements.

  • Returns a negative value if the first array is lexicographically smaller.
  • Returns zero if both arrays are equal.
  • Returns a positive value if the first array is lexicographically greater.
Java
import java.util.Arrays;

public class Geeks{
    public static void main(String[] args){

        int[] arr1 = {10, 20, 15, 22, 35};
        int[] arr2 = {10, 15, 22};

        System.out.println("Comparison result: " + Arrays.compare(arr1, arr2));
    }
}

Output
Comparison result: 1

Explanation: The first elements of both arrays are equal. At index 1, the value 20 in arr1 is greater than 15 in arr2, so the method returns a positive value.

5. Other Common Array Operations

The Arrays class also supports copying, filling, comparing, and converting arrays.

Java
import java.util.Arrays;

public class Geeks{
    public static void main(String[] args){

        int[] arr = {10, 20, 30, 40, 50};

        // Copy an array
        int[] copy = Arrays.copyOf(arr, arr.length);
        System.out.println("Copied Array: " + Arrays.toString(copy));

        // Compare array contents
        System.out.println("Equal Arrays: " + Arrays.equals(arr, copy));

        // Copy a specified range
        int[] range = Arrays.copyOfRange(arr, 1, 4);
        System.out.println("Range: " + Arrays.toString(range));

        // Fill an array with a value
        Arrays.fill(arr, 5);
        System.out.println("Filled Array: " + Arrays.toString(arr));
    }
}

Output
Copied Array: [10, 20, 30, 40, 50]
Equal Arrays: true
Range: [20, 30, 40]
Filled Array: [5, 5, 5, 5, 5]

Explanation:

  • copyOf() creates a new array with the specified length.
  • equals() compares the contents of two arrays.
  • copyOfRange() copies elements within the specified index range.
  • fill() assigns the specified value to every element of the array.

Methods of Arrays Class

The following table lists the important methods of the Arrays class, including their commonly used overloads.

Method

Description

asList(array)

Returns a fixed-size list backed by the specified array.

binarySearch(array, key)

Searches for an element in a sorted array.

binarySearch(array, fromIndex, toIndex, key)

Searches within a specified sorted range of an array.

compare(array1, array2)

Compares two arrays lexicographically.

compareUnsigned(array1, array2)

Compares supported integer arrays lexicographically using unsigned values.

copyOf(array, newLength)

Copies an array, truncating or padding it with default values if necessary.

copyOfRange(array, fromIndex, toIndex)

Copies the specified range into a new array.

deepEquals(array1, array2)

Compares the contents of nested arrays recursively.

deepHashCode(array)

Returns a hash code based on the deep contents of an array.

deepToString(array)

Returns a string representation of nested array contents.

equals(array1, array2)

Checks whether two arrays contain equal corresponding elements in the same order.

fill(array, value)

Assigns the specified value to every element.

fill(array, fromIndex, toIndex, value)

Assigns a value to elements within the specified range.

hashCode(array)

Returns a hash code based on the array contents.

mismatch(array1, array2)

Returns the index of the first difference or -1 if the arrays are equal.

parallelPrefix(array, operator)

Performs cumulative operations on array elements in parallel using an associative operator.

parallelPrefix(array, fromIndex, toIndex, operator)

Performs parallel prefix operations within a specified range.

parallelSetAll(array, generator)

Sets array elements in parallel using a generator function.

parallelSort(array)

Sorts an array using a parallel sorting algorithm where appropriate.

setAll(array, generator)

Sets array elements using a generator function.

sort(array)

Sorts the array in ascending or natural order.

sort(array, fromIndex, toIndex)

Sorts the specified range of an array.

sort(array, comparator)

Sorts an object array according to the specified comparator.

sort(array, fromIndex, toIndex, comparator)

Sorts a specified range of an object array using a comparator.

spliterator(array)

Returns a Spliterator covering the specified array.

spliterator(array, fromIndex, toIndex)

Returns a Spliterator covering the specified range.

stream(array)

Returns a sequential stream for supported array types.

stream(array, fromIndex, toIndex)

Returns a sequential stream covering the specified range.

toString(array)

Returns a string representation of the array elements enclosed in square brackets.

Note: For methods that accept index ranges, fromIndex is inclusive and toIndex is exclusive. Method availability and supported array types depend on the specific overload.

Comment