C

Core Java tutorial for beginners

Clean • Professional

Core Java Arrays – Arrays Utility Methods in Java (With Examples)

4 minute

Arrays Utility Methods in Java

When working with arrays, Java provides a powerful helper class — java.util.Arrays.

This class contains static utility methods that make it easier to perform common array operations like:

  • Converting arrays to readable strings
  • Comparing arrays
  • Filling arrays with default values
  • Sorting arrays efficiently

Using these methods helps write cleaner, faster, and more readable code.

learn code with durgesh images

1. Arrays.toString() – Convert Array to Readable String

Normally, printing an array directly like this:

int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers);

prints something like:

[I@15db9742

That’s the array’s memory reference, not its content.

To print the actual elements, use Arrays.toString():

import java.util.Arrays;

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

        System.out.println("Array: " + Arrays.toString(numbers));
    }
}

Output:

Array: [1, 2, 3, 4, 5]

Notes:

  • Converts array elements into a single string format.
  • Works for all primitive and object arrays.
  • For multidimensional arrays, use Arrays.deepToString() instead.

Example (Multidimensional):

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

Output:

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

2. Arrays.equals() – Compare Two Arrays

The equals() method checks whether two arrays contain the same elements in the same order.

import java.util.Arrays;

public class EqualsExample {
    public static void main(String[] args) {
        int[] arr1 = {10, 20, 30};
        int[] arr2 = {10, 20, 30};
        int[] arr3 = {30, 20, 10};

        System.out.println(Arrays.equals(arr1, arr2)); // true
        System.out.println(Arrays.equals(arr1, arr3)); // false
    }
}

Output:

true
false

Notes:

  • Returns true only if both arrays have equal length and elements.
  • For multidimensional arrays, use Arrays.deepEquals().

Example (Nested Arrays):

String[][] names1 = {{"Aman", "Ravi"}, {"Neha", "Tina"}};
String[][] names2 = {{"Aman", "Ravi"}, {"Neha", "Tina"}};

System.out.println(Arrays.deepEquals(names1, names2)); // true

3. Arrays.fill() – Fill Array with a Specific Value

Used to initialize or reset all elements of an array with a single value.

import java.util.Arrays;

public class FillExample {
    public static void main(String[] args) {
        int[] marks = new int[5];
        Arrays.fill(marks, 100);

        System.out.println("Filled Array: " + Arrays.toString(marks));
    }
}

Output:

Filled Array: [100, 100, 100, 100, 100]

Notes:

  • Great for default initialization.
  • Can also fill a range using Arrays.fill(array, fromIndex, toIndex, value)

Example (Range Fill):

int[] scores = {10, 20, 30, 40, 50};
Arrays.fill(scores, 1, 4, 99);
System.out.println(Arrays.toString(scores));

Output:

[10, 99, 99, 99, 50]

4. Arrays.sort() – Sort Array in Ascending Order

The Arrays.sort() method is used to sort elements of an array in ascending order by default.

import java.util.Arrays;

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

        Arrays.sort(numbers);

        System.out.println("Sorted Array: " + Arrays.toString(numbers));
    }
}

Output:

Sorted Array: [1, 2, 3, 5, 8]

Notes:

  • Works for primitive arrays and object arrays.
  • For descending order: use Collections.reverseOrder() (works for Integer[], not int[]).

Descending Example:

import java.util.*;

public class DescendingExample {
    public static void main(String[] args) {
        Integer[] numbers = {5, 2, 8, 1, 3};
        Arrays.sort(numbers, Collections.reverseOrder());
        System.out.println("Descending Order: " + Arrays.toString(numbers));
    }
}

Points to Remember

  • Always use deepToString() and deepEquals() for nested arrays.
  • Arrays.fill() is useful for default initialization.
  • Arrays.sort() is optimized and very fast for large arrays.
  • All these methods are static, so you can call them directly using the class name.

🏆 Top 5 Interview Questions – Arrays Utility Methods in Java

1. What is the difference between Arrays.equals() and Arrays.deepEquals()?

Answer:

  • Arrays.equals() → Compares one-dimensional arrays only.
  • Arrays.deepEquals() → Compares nested or multidimensional arrays.

Example:

int[][] arr1 = {{1,2}, {3,4}};
int[][] arr2 = {{1,2}, {3,4}};
System.out.println(Arrays.equals(arr1, arr2));     // false
System.out.println(Arrays.deepEquals(arr1, arr2)); // true

2. How do you print arrays in Java?

Answer:

  • Use Arrays.toString(array) for 1D arrays.
  • Use Arrays.deepToString(array) for multidimensional arrays.

Example:

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

Output:

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

3. Can you fill only part of an array using Arrays.fill()?

Answer: Yes, using the range version:

int[] arr = {10, 20, 30, 40, 50};
Arrays.fill(arr, 1, 4, 99);
System.out.println(Arrays.toString(arr));

Output:

[10, 99, 99, 99, 50]

4. How do you sort an array in descending order?

Answer:

  • Use Arrays.sort() with Collections.reverseOrder() for object arrays like Integer[].
  • Primitive arrays (int[]) must be converted to Integer[] first.

Example:

Integer[] numbers = {5, 2, 8, 1, 3};
Arrays.sort(numbers, Collections.reverseOrder());
System.out.println(Arrays.toString(numbers));

Output:

[8, 5, 3, 2, 1]

5. What are some important points about Arrays utility methods?

Answer:

  1. Methods are static, no need to create an instance.
  2. deepToString() and deepEquals() are used for nested arrays.
  3. Arrays.sort() is optimized and fast, works for primitive & object arrays.
  4. Arrays.fill() helps in initializing arrays quickly.
  5. Always remember the difference between primitive arrays and object arrays when using methods like sort() or reverseOrder().

Article 0 of 0