Java Arrays Class

The Java Arrays class is a utility class in java.util that provides helpful methods for working with arrays. It can print arrays in a readable format, sort values, search sorted arrays, fill arrays, compare arrays, and make copies.

Because Arrays is in the standard library, you usually import it with import java.util.Arrays; before using its methods.

Print An Array

If you print an array variable directly, Java does not print its elements. Use Arrays.toString() to get a readable string for a one-dimensional array.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] scores = {90, 85, 92};

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

Output:

[90, 85, 92]

The result includes square brackets and commas, which makes it useful for quick output and debugging.

Sort An Array

Use Arrays.sort() to arrange an array in ascending order. For numbers, that means smallest to largest. For strings, it means alphabetical order.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] numbers = {5, 2, 9, 1, 3};

        Arrays.sort(numbers);

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

Output:

[1, 2, 3, 5, 9]

Arrays.sort() changes the original array. It does not create a new sorted array.

Search A Sorted Array

Use Arrays.binarySearch() to find the index of a value in a sorted array. The array should be sorted first, or the result may be wrong.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] names = {"Mia", "Ava", "Noah", "Liam"};

        Arrays.sort(names);
        int index = Arrays.binarySearch(names, "Noah");

        System.out.println(Arrays.toString(names));
        System.out.println("Noah is at index " + index);
    }
}

Output:

[Ava, Liam, Mia, Noah]
Noah is at index 3

After sorting, Noah is at index 3. If the value is not found, binarySearch() returns a negative number.

Fill An Array

Use Arrays.fill() when every element should start with the same value. This is often simpler than writing a loop.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] counts = new int[5];

        Arrays.fill(counts, 1);

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

Output:

[1, 1, 1, 1, 1]

The array has five elements, and each element is set to 1.

Compare Arrays

The == operator checks whether two array variables refer to the same array object. To compare the elements inside two arrays, use Arrays.equals().

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[] first = {1, 2, 3};
        int[] second = {1, 2, 3};

        System.out.println(first == second);
        System.out.println(Arrays.equals(first, second));
    }
}

Output:

false
true

The arrays contain the same values in the same order, so Arrays.equals() returns true.

Copy Part Of An Array

Use Arrays.copyOf() to make a new array with a chosen length. If the new length is shorter, extra values are left out. If it is longer, Java adds default values.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] tools = {"javac", "java", "jshell"};

        String[] firstTwo = Arrays.copyOf(tools, 2);
        String[] longer = Arrays.copyOf(tools, 5);

        System.out.println(Arrays.toString(firstTwo));
        System.out.println(Arrays.toString(longer));
    }
}

Output:

[javac, java]
[javac, java, jshell, null, null]

Because tools is a String[], the added elements in the longer copy are null.

Multidimensional Arrays

For multidimensional arrays, use Arrays.deepToString() to print nested arrays in a readable way.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[][] grid = {
            {1, 2},
            {3, 4}
        };

        System.out.println(Arrays.deepToString(grid));
    }
}

Output:

[[1, 2], [3, 4]]

Use toString() for one-dimensional arrays and deepToString() for arrays that contain other arrays.

Common Arrays Methods

Method Use
Arrays.toString(array) Creates readable text for a one-dimensional array.
Arrays.sort(array) Sorts the original array.
Arrays.binarySearch(array, value) Searches a sorted array and returns an index.
Arrays.fill(array, value) Sets every element to the same value.
Arrays.equals(a, b) Compares two arrays element by element.
Arrays.copyOf(array, length) Creates a new array copy with the given length.

Takeaway: the Arrays class gives you ready-made methods for common array tasks, so you do not have to write every loop by hand.