subarray()?subarray() is a ArrayUtils class. It returns an array containing the elements of the given array between the start index inclusive and the end index exclusive.
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.
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.
ArrayUtilsYou can import the ArrayUtils class as follows:
import org.apache.commons.lang3.ArrayUtils;
public static int[] subarray(final int[] array, int startIndexInclusive, int endIndexExclusive)
final int[] array: given array.
int startIndexInclusive: start index.
int endIndexExclusive: end index.
A new array containing the subset of the elements of the given array.
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);}}
array = [1,2,3,4,5]start index = 0end index = 3The array [1,2,3] is a result of applying the subarray function, because the start index is inclusive and the end index is exclusive.
array = [1,2,3,4,5]start index = -3end index = 3The array [1,2,3] is a result of applying the subarray function, because the start index would be changed to zero.
array = [1,2,3,4,5]start index = 10end index = 20Applying the subarray function will result in an empty array, because the start index is greater than the length of the array.
array = [1,2,3,4,5]start index = 0end index = -5Applying the subarray function will result in an empty array, because the end index is less than the start index.
array = [1,2,3,4,5]start index = 0end index = 10The 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.
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 ]