The Arrays class in java.util package is a utility class that provides static methods to perform operations on Java arrays, such as sorting, searching, comparing and converting to strings.
The class hierarchy is as follows:
Arrays class is a utility class in java.util package that extends Object
Array ClassWhy do we need the Java Arrays class?
Java provides a utility class called java.util.Arrays to help developers perform common array operations easily and efficiently. like:
- Fill an array with a particular value.
 - Sort an array
 - Search in an array
 - And many more
 
Class Declaration  
Arrays is a final utility class in java.util package that extends Object class, which is the root of the Java class hierarchy
public class Arrays extends Object
To use Arrays,
Arrays.<function name>;
Arrays.sort(array_name);
Methods in Java Array Class 
The Arrays class of the java.util package contains several static methods that can be used to fill, sort, search, etc in arrays. Let's take a look at methods and their implementation:
Java Arrays Class Methods, Explained with Examples
Example 1: asList() method.
This method converts an array into a list.
 
            Java
    // Importing Arrays utility class
// from java.util package 
import java.util.Arrays;
// Main class 
class Geeks {
  
    // Main driver method 
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // To convert the elements as List
        System.out.println("Integer Array as List: "
                           + Arrays.asList(intArr));
    }
}
OutputInteger Array as List: [[I@19469ea2]
 Note: When asList() is used with primitive arrays, it shows the memory reference of the array instead of the list contents. This happens because the asList() method returns a fixed-size list backed by the original array, and for primitive types like int[], it treats the array as an object, not as a list of values.
Example 2: binarySearch() Method
This methods search for the specified element in the array with the help of the binary search algorithm.
            Java
    import java.util.Arrays;
// Main class
public class Geeks {
    // Main driver method
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        Arrays.sort(intArr);
        int intKey = 22;
        // Print the key and corresponding index
        System.out.println(
            intKey + " found at index = "
            + Arrays.binarySearch(intArr, intKey));
    }
}
Output22 found at index = 3
 Example 3: binarySearch(array, fromIndex, toIndex, key, Comparator) Method 
This method searches a range of the specified array for the specified object using the binary search algorithm.
            Java
    import java.util.Arrays;
public class Main {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        Arrays.sort(intArr);
        int intKey = 22;
        System.out.println(
            intKey
            + " found at index = "
            + Arrays
                  .binarySearch(intArr, 1, 3, intKey));
    }
}
Output22 found at index = -4
 Example 4: compare(array 1, array 2) Method 
This method returns the difference as an integer lexicographically.
            Java
    import java.util.Arrays;
public class Main {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // Get the second Array
        int intArr1[] = { 10, 15, 22 };
        // To compare both arrays
        System.out.println("Integer Arrays on comparison: "
                           + Arrays.compare(intArr, intArr1));
    }
}
OutputInteger Arrays on comparison: 1
 Example 5: compareUnsigned(array 1, array 2) Method 
This method is used to compare two arrays of primitive int[] values (or other primitive array types) lexicographically, but with the values treated as unsigned integers.
            Java
    import java.util.Arrays;
public class Main {
    public static void main(String[] args)
    {
        // Get the Arrays
        int intArr[] = { 10, 20, 15, 22, 35 };
        // Get the second Arrays
        int intArr1[] = { 10, 15, 22 };
        // To compare both arrays
        System.out.println("Integer Arrays on comparison: "
                           + Arrays.compareUnsigned(intArr, intArr1));
    }
}
OutputInteger Arrays on comparison: 1
 Example 6: copyOf(originalArray, newLength) Method 
This method is used to copy the elements of an array into a new array of the specified new length. 
            Java
    import java.util.Arrays;
public class Geeks{
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // To print the elements in one line
        System.out.println("Integer Array: "
                           + Arrays.toString(intArr));
        System.out.println("\nNew Arrays by copyOf:\n");
        System.out.println("Integer Array: "
                           + Arrays.toString(
                                 Arrays.copyOf(intArr, 10)));
    }
}
OutputInteger Array: [10, 20, 15, 22, 35]
New Arrays by copyOf:
Integer Array: [10, 20, 15, 22, 35, 0, 0, 0, 0, 0]
 Example 7: copyOfRange(originalArray, fromIndex, endIndex) Method 
This method is used to copy a range of elements from an array into a new array.
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // To print the elements in one line
        System.out.println("Integer Array: "
                           + Arrays.toString(intArr));
        System.out.println("\nNew Arrays by copyOfRange:\n");
        // To copy the array into an array of new length
        System.out.println("Integer Array: "
                           + Arrays.toString(
                                 Arrays.copyOfRange(intArr, 1, 3)));
    }
}
OutputInteger Array: [10, 20, 15, 22, 35]
New Arrays by copyOfRange:
Integer Array: [20, 15]
 Example 8: deepEquals(Object[] a1, Object[] a2) Method
This method is used to compare two arrays of objects to check if they are deeply equal.
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Arrays
        int intArr[][] = { { 10, 20, 15, 22, 35 } };
        // Get the second Arrays
        int intArr1[][] = { { 10, 15, 22 } };
        // To compare both arrays
        System.out.println("Integer Arrays on comparison: "
                           + Arrays.deepEquals(intArr, intArr1));
    }
}
OutputInteger Arrays on comparison: false
 Example 9: deepHashCode(Object[] a) Method 
This method is used to compute a hash code for an array of objects recursively.
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[][] = { { 10, 20, 15, 22, 35 } };
        // To get the dep hashCode of the arrays
        System.out.println("Integer Array: "
                           + Arrays.deepHashCode(intArr));
    }
}
OutputInteger Array: 38475344
 Example 10: deepToString(Object[] a) Method 
This method is used to return a string representation of an array recursively.
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[][] = { { 10, 20, 15, 22, 35 } };
        // To get the deep String of the arrays
        System.out.println("Integer Array: "
                           + Arrays.deepToString(intArr));
    }
}
OutputInteger Array: [[10, 20, 15, 22, 35]]
 Example 11: equals(array1, array2) Method 
This method is used to compare two arrays to check if they are equal.
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Arrays
        int intArr[] = { 10, 20, 15, 22, 35 };
        // Get the second Arrays
        int intArr1[] = { 10, 15, 22 };
        // To compare both arrays
        System.out.println("Integer Arrays on comparison: "
                           + Arrays.equals(intArr, intArr1));
    }
}
OutputInteger Arrays on comparison: false
 Example 12: fill(originalArray, fillValue) Method 
This method is used to fill an entire array or a subrange of an array with a specific value.
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Arrays
        int intArr[] = { 10, 20, 15, 22, 35 };
        int intKey = 22;
        Arrays.fill(intArr, intKey);
        // To fill the arrays
        System.out.println("Integer Array on filling: "
                           + Arrays.toString(intArr));
    }
}
OutputInteger Array on filling: [22, 22, 22, 22, 22]
 Example 13: hashCode(originalArray) Method 
This method is used to compute a hash code for an array
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // To get the hashCode of the arrays
        System.out.println("Integer Array: "
                           + Arrays.hashCode(intArr));
    }
}
OutputInteger Array: 38475313
 Example 14: mismatch(array1, array2) Method
This method is used to find the index of the first mismatched element between two arrays
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Arrays
        int intArr[] = { 10, 20, 15, 22, 35 };
        // Get the second Arrays
        int intArr1[] = { 10, 15, 22 };
        // To compare both arrays
        System.out.println("The element mismatched at index: "
                           + Arrays.mismatch(intArr, intArr1));
    }
}
OutputThe element mismatched at index: 1
 Example 15: parallelSort(originalArray) Method
This method is used to sort an array in parallel.
            Java
     import java.util.Arrays;
// Main class
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // To sort the array using parallelSort
        Arrays.parallelSort(intArr);
        System.out.println("Integer Array: "
                           + Arrays.toString(intArr));
    }
}
OutputInteger Array: [10, 15, 20, 22, 35]
 Example 16: sort(originalArray) Method 
This method is used to sort an array in ascending order
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // To sort the array using normal sort-
        Arrays.sort(intArr);
        System.out.println("Integer Array: "
                           + Arrays.toString(intArr));
    }
}
OutputInteger Array: [10, 15, 20, 22, 35]
 Example 17: sort(originalArray, fromIndex, endIndex) Method
This method is used to sort a specified range of an array in ascending order 
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // To sort the array using normal sort
        Arrays.sort(intArr, 1, 3);
        System.out.println("Integer Array: "
                           + Arrays.toString(intArr));
    }
}
OutputInteger Array: [10, 15, 20, 22, 35]
 Example 18: sort(T[] a, int fromIndex, int toIndex, Comparator< super T> c) Method 
This method is used to sort a specified range of an array using a custom comparator for sorting.
            Java
    import java.util.*;
import java.lang.*;
import java.io.*;
// A class to represent a student.
class Student {
    int rollno;
    String name, address;
    // Constructor
    public Student(int rollno, String name,
                   String address)
    {
        this.rollno = rollno;
        this.name = name;
        this.address = address;
    }
    // Used to print student details in main()
    public String toString()
    {
        return this.rollno + " "
            + this.name + " "
            + this.address;
    }
}
class Sortbyroll implements Comparator<Student> {
    // Used for sorting in ascending order of
    // roll number
    public int compare(Student a, Student b)
    {
        return a.rollno - b.rollno;
    }
}
// Driver class
class Geeks {
    public static void main(String[] args)
    {
        Student[] arr = { new Student(111, "bbbb", "london"),
                          new Student(131, "aaaa", "nyc"),
                          new Student(121, "cccc", "jaipur") };
        System.out.println("Unsorted");
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);
        Arrays.sort(arr, 1, 2, new Sortbyroll());
        System.out.println("\nSorted by rollno");
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);
    }
}
OutputUnsorted
111 bbbb london
131 aaaa nyc
121 cccc jaipur
Sorted by rollno
111 bbbb london
131 aaaa nyc
121 cccc jaipur
 Example 19: sort(T[] a, Comparator< super T> c) Method
This method is used to sort an entire array of objects (T[]) using a custom comparator (Comparator<? super T>).
            Java
    import java.util.*;
import java.lang.*;
import java.io.*;
// A class to represent a student.
class Student {
    int rollno;
    String name, address;
    // Constructor
    public Student(int rollno, String name,
                   String address)
    {
        this.rollno = rollno;
        this.name = name;
        this.address = address;
    }
    // Used to print student details in main()
    public String toString()
    {
        return this.rollno + " "
            + this.name + " "
            + this.address;
    }
}
class Sortbyroll implements Comparator<Student> {
    // Used for sorting in ascending order of roll number
    public int compare(Student a, Student b)
    {
        return a.rollno - b.rollno;
    }
}
// Driver class
class Geeks {
    public static void main(String[] args)
    {
        Student[] arr = { new Student(111, "bbbb", "london"),
                          new Student(131, "aaaa", "nyc"),
                          new Student(121, "cccc", "jaipur") };
        System.out.println("Unsorted");
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);
        Arrays.sort(arr, new Sortbyroll());
        System.out.println("\nSorted by rollno");
        for (int i = 0; i < arr.length; i++)
            System.out.println(arr[i]);
    }
}
OutputUnsorted
111 bbbb london
131 aaaa nyc
121 cccc jaipur
Sorted by rollno
111 bbbb london
121 cccc jaipur
131 aaaa nyc
 Example 20: spliterator(originalArray) Method 
This method is used to create a Spliterator for the given array.
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // To sort the array using normal sort
        System.out.println("Integer Array: "
                           + Arrays.spliterator(intArr));
    }
}
OutputInteger Array: java.util.Spliterators$IntArraySpliterator@4e50df2e
 Example 21: spliterator(originalArray, fromIndex, endIndex) Method 
This method is used to create a Spliterator for a subrange of the given array, starting from fromIndex (inclusive) to toIndex (exclusive).
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // To sort the array using normal sort
        System.out.println("Integer Array: "
                           + Arrays.spliterator(intArr, 1, 3));
    }
}
OutputInteger Array: java.util.Spliterators$IntArraySpliterator@4e50df2e
 Example 22: stream(originalArray) Method
This method is used to convert an array into a stream.
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // To get the Stream from the array
        System.out.println("Integer Array: "
                           + Arrays.stream(intArr));
    }
}
OutputInteger Array: java.util.stream.IntPipeline$Head@7291c18f
 Example 23: toString(originalArray) Method 
This method is used to convert an array into a human-readable string representation.
            Java
    import java.util.Arrays;
public class Geeks {
    public static void main(String[] args)
    {
        // Get the Array
        int intArr[] = { 10, 20, 15, 22, 35 };
        // To print the elements in one line
        System.out.println("Integer Array: "
                           + Arrays.toString(intArr));
    }
}
OutputInteger Array: [10, 20, 15, 22, 35]
 Below table contains list of all methods:
| Methods  | Action Performed | 
|---|
| asList() | Returns a fixed-size list backed by the specified Arrays   | 
| binarySearch() | Searches for the specified element in the array with the help of the Binary Search Algorithm | 
| binarySearch(array, fromIndex, toIndex, key, Comparator) | Searches a range of the specified array for the specified object using the Binary Search Algorithm | 
| compare(array 1, array 2) | Compares two arrays passed as parameters lexicographically. | 
| copyOf(originalArray, newLength) | Copies the specified array, truncating or padding with the default value (if necessary) so the copy has the specified length. | 
| copyOfRange(originalArray, fromIndex, endIndex) | Copies the specified range of the specified array into a new Arrays. | 
| deepEquals(Object[] a1, Object[] a2) | Returns true if the two specified arrays are deeply equal to one another. | 
| deepHashCode(Object[] a)  | Returns a hash code based on the "deep contents" of the specified Arrays. | 
| deepToString(Object[] a) | Returns a string representation of the "deep contents" of the specified Arrays. | 
| equals(array1, array2) | Checks if both the arrays are equal or not. | 
| fill(originalArray, fillValue) | Assigns this fill value to each index of this arrays. | 
| hashCode(originalArray)  | Returns an integer hashCode of this array instance. | 
| mismatch(array1, array2)  | Finds and returns the index of the first unmatched element between the two specified arrays. | 
| parallelPrefix(originalArray, fromIndex, endIndex, functionalOperator) | Performs parallelPrefix for the given range of the array with the specified functional operator. | 
| parallelPrefix(originalArray, operator) | Performs parallelPrefix for complete array with the specified functional operator.  | 
| parallelSetAll(originalArray, functionalGenerator) | Sets all the elements of this array in parallel, using the provided generator function.  | 
| parallelSort(originalArray) | Sorts the specified array using parallel sort. | 
| setAll(originalArray, functionalGenerator) | Sets all the elements of the specified array using the generator function provided.  | 
| sort(originalArray) | Sorts the complete array in ascending order.  | 
| sort(originalArray, fromIndex, endIndex) | Sorts the specified range of array in ascending order. | 
| sort(T[] a, int fromIndex, int toIndex, Comparator< super T> c) | Sorts the specified range of the specified array of objects according to the order induced by the specified comparator. | 
| sort(T[] a, Comparator< super T> c) | Sorts the specified array of objects according to the order induced by the specified comparator. | 
| spliterator(originalArray) | Returns a Spliterator covering all of the specified Arrays. | 
| spliterator(originalArray, fromIndex, endIndex)  | Returns a Spliterator of the type of the array covering the specified range of the specified arrays. | 
| stream(originalArray)  | Returns a sequential stream with the specified array as its source. | 
| toString(originalArray)  | It returns a string representation of the contents of this array. The string representation consists of a list of the array’s elements, enclosed in square brackets (“[]”). Adjacent elements are separated by the characters a comma followed by a space. Elements are converted to strings as by String.valueOf() function. | 
                                
                                
                            
                                                                                
                                                            
                                                        
                            
                        
                                                
                        
                                                                                    
                                                                Explore
                                    
                                        Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java