ArrayUtils

About

ArrayUtils is a utility class provided by Apache Commons Lang to simplify working with Java arrays. Java arrays are low-level and have limited built-in methods, making operations like searching, copying, checking for emptiness, or merging somewhat cumbersome.

ArrayUtils provides null-safe, convenient, and readable methods to handle arrays of all primitive types and objects. It also helps avoid manual loops, System.arraycopy, and verbose boilerplate code.

Characteristics

  • All methods are static.

  • Handles both primitive and object arrays.

  • Offers null safety — no need to check for null before calling utility methods.

  • Reduces common array operations to one-liners.

Maven Dependency & Import

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version> <!-- or latest -->
</dependency>
import org.apache.commons.lang3.ArrayUtils;

Common Categories of Methods

1. Null and Empty Checks

Method
Description

isEmpty(array)

Returns true if array is null or length is 0

isNotEmpty(array)

Returns true if array is not null and has elements

Example:

if (ArrayUtils.isEmpty(myArray)) {
    // safely handle empty case
}

2. Array Creation and Conversion

Method
Description

toObject(int[])

Converts a primitive array to wrapper array

toPrimitive(Integer[])

Converts wrapper array to primitive array

nullToEmpty(array)

Converts null array to empty array

Example:

int[] ints = {1, 2, 3};
Integer[] boxed = ArrayUtils.toObject(ints);

3. Element Checks

Method
Description

contains(array, value)

Checks if array contains a specific element

indexOf(array, value)

Returns the index of the element, or -1

lastIndexOf(array, value)

Returns the last index of the element

Example:

if (ArrayUtils.contains(new int[]{1, 2, 3}, 2)) {
    // element found
}

4. Adding and Removing Elements

Method
Description

add(array, value)

Adds value at the end

add(array, index, value)

Inserts value at a specific index

remove(array, index)

Removes value at index

removeElement(array, value)

Removes first occurrence of value

Example:

int[] result = ArrayUtils.add(new int[]{1, 2}, 3); // [1, 2, 3]

5. Concatenation and Cloning

Method
Description

addAll(array1, array2)

Concatenates arrays

clone(array)

Returns a clone of the array

subarray(array, startIndex, endIndex)

Gets a range of elements

Example:

int[] combined = ArrayUtils.addAll(new int[]{1, 2}, new int[]{3, 4});

6. Array Reversal and Shuffling

Method
Description

reverse(array)

Reverses the array in place

shuffle(array)

Randomizes the order (not in standard ArrayUtils, but can use custom logic)

Example:

int[] nums = {1, 2, 3};
ArrayUtils.reverse(nums); // nums becomes {3, 2, 1}

7. Equality and Comparison

Method
Description

isSameLength(array1, array2)

Checks if two arrays are of the same length

isEquals(array1, array2)

Checks if all elements are equal

8. Array Type Checking

Method
Description

isArray(Object obj)

Checks whether a given object is an array

Why Use ArrayUtils Instead of Raw Java?

Concern

Raw Java Approach

ArrayUtils Advantage

Null-safety

Manual checks

Built-in null checks

Adding/removing

Manual resizing or arraycopy

Simple one-liners

Searching

Manual loops

Ready-made methods

Conversion

Tedious type boxing/unboxing

Simplified conversions

Last updated

Was this helpful?