What is ArrayUtils.subarray in Java?

What is subarray()?

subarray() is a staticAny static member can be accessed before objects of its class are created and without reference to an object. method of the ArrayUtils class. It returns an array containing the elements of the given array between the start index inclusive and the end index exclusive.

Properties of subarray()

  • The method returns null if the given array is null.

  • The start index value will be changed to zero if the start index is less than zero.

  • The method returns an empty array in the following scenarios:

    • The start index is greater than the length of the array.

    • The end index is less than the start index.

  • The end index value will be changed to the length of the array, if the end index is greater than the length of the array.

Add apache commons-lang package

ArrayUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml file.


<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

Import ArrayUtils

You can import the ArrayUtils class as follows:

import org.apache.commons.lang3.ArrayUtils;

Syntax


public static int[] subarray(final int[] array, int startIndexInclusive, int endIndexExclusive)

Parameters

  • final int[] array: given array.

  • int startIndexInclusive: start index.

  • int endIndexExclusive: end index.

Returns

A new array containing the subset of the elements of the given array.

Code

The code below is made from different examples.

import org.apache.commons.lang3.ArrayUtils;
public class Main {
private static void subArray(int[] array, int startIndex, int endIndex){
int[] result = ArrayUtils.subarray(array, startIndex, endIndex);
System.out.print("SubArray for " + startIndex + " and " + endIndex + " is [ ");
for(int i: result) System.out.print(i + " ");
System.out.print("]");
System.out.println();
}
public static void main(String[] args) {
int[] array = {1,2,3,4,5};
System.out.print("Original Array is - [ ");
for(int i:array) System.out.print( i + " ");
System.out.println("]");
subArray(array, 0, 3);
subArray(array, -1, 3);
subArray(array, 10, 20);
subArray(array, 0, -5);
subArray(array, 0, 10);
}
}

Example 1

  • array = [1,2,3,4,5]
  • start index = 0
  • end index = 3

The array [1,2,3] is a result of applying the subarray function, because the start index is inclusive and the end index is exclusive.

Example 2

  • array = [1,2,3,4,5]
  • start index = -3
  • end index = 3

The array [1,2,3] is a result of applying the subarray function, because the start index would be changed to zero.

Example 3

  • array = [1,2,3,4,5]
  • start index = 10
  • end index = 20

Applying the subarray function will result in an empty array, because the start index is greater than the length of the array.

Example 4

  • array = [1,2,3,4,5]
  • start index = 0
  • end index = -5

Applying the subarray function will result in an empty array, because the end index is less than the start index.

Example 5

  • array = [1,2,3,4,5]
  • start index = 0
  • end index = 10

The array [1,2,3,4,5] is a result of applying the subarray function, because the end index value would get changed to the array length.

Output


Original Array is - [ 1 2 3 4 5 ]
SubArray for 0 and 3 is [ 1 2 3 ]
SubArray for -1 and 3 is [ 1 2 3 ]
SubArray for 10 and 20 is [ ]
SubArray for 0 and -5 is [ ]
SubArray for 0 and 10 is [ 1 2 3 4 5 ]

Free Resources