Java Arrays Utility Class: Sorting, Searching, Copying and Printing

The java.util.Arrays class is a powerful utility toolkit containing static helper methods for manipulating, sorting, searching, comparing, and printing Java arrays efficiently.

Essential java.util.Arrays Methods

import java.util.Arrays;

public class ArraysUtilityDemo {
    public static void main(String[] args) {
        int[] scores = { 85, 42, 99, 14, 76 };

        // 1. Arrays.toString() - Clean formatted printing:
        System.out.println("Original: " + Arrays.toString(scores)); // [85, 42, 99, 14, 76]

        // 2. Arrays.sort() - High-speed Dual-Pivot Quicksort:
        Arrays.sort(scores);
        System.out.println("Sorted:   " + Arrays.toString(scores)); // [14, 42, 76, 85, 99]

        // 3. Arrays.binarySearch() - O(log n) lookup (Requires sorted array!):
        int index = Arrays.binarySearch(scores, 76);
        System.out.println("Index of 76: " + index); // Index 2

        // 4. Arrays.copyOf() - Deep cloning and resizing:
        int[] expanded = Arrays.copyOf(scores, 7);
        System.out.println("Expanded (length 7): " + Arrays.toString(expanded)); // [14, 42, 76, 85, 99, 0, 0]

        // 5. Arrays.fill() - Batch initialization:
        int[] flags = new int[5];
        Arrays.fill(flags, 1);
        System.out.println("Filled flags: " + Arrays.toString(flags)); // [1, 1, 1, 1, 1]

        // 6. Arrays.equals() - Value-based equality:
        int[] a1 = { 1, 2, 3 };
        int[] a2 = { 1, 2, 3 };
        System.out.println("Arrays equal: " + Arrays.equals(a1, a2)); // true
    }
}

Multidimensional Array Printing: Arrays.deepToString()

Calling standard Arrays.toString() on a 2D array merely prints internal memory hashcodes. Use Arrays.deepToString() for formatted 2D/3D grids:

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

University Exam Corner (BCA / B.Sc CS)

Exam Question: "What does Arrays.binarySearch() return if the target element does NOT exist in the array?"

Answer: It returns a negative integer: -(insertion point) - 1, where the "insertion point" is the index position where the element would have been inserted to maintain sorted order. For example, if searching for 50 in [14, 42, 76, 85, 99], it should go at index 2, so it returns -(2) - 1 = -3.

Next Step

Now let us explore how the terminal passes arguments to a Java program via String[] args. Proceed to: Command Line Arguments →

Frequently Asked Questions (FAQ)

Q1. What sorting algorithm does Arrays.sort() use in Java?
For primitive types (int, double, etc.), Arrays.sort() uses Dual-Pivot Quicksort (O(n log n)). For reference Object arrays, it uses TimSort (an adaptive, stable hybrid of MergeSort and InsertionSort).
Q2. What is the prerequisite before invoking Arrays.binarySearch()?
The array MUST be sorted in ascending order before calling Arrays.binarySearch(). If the array is unsorted, the search result is completely undefined.
Q3. What is the difference between Arrays.equals() and Arrays.deepEquals()?
Arrays.equals() performs shallow comparison on 1D arrays. Arrays.deepEquals() recursively inspects nested multidimensional arrays (e.g. 2D matrices) comparing nested elements for value equality.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

Java Arrays Basics →

Array creation and memory.

Collections Utility Class →

Sorting and searching lists.

Comparable vs Comparator →

Custom object sorting.

Command Line Arguments →

Terminal string arrays.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!