Get a subarray of an array between specific index in Java
This post will discuss several methods to get a subarray of a non-primitive array between specified indices in Java.
1. Using Arrays.copyOfRange() method
The standard way to get a subarray of an array is to use the Arrays.copyOfRange(), which returns a subarray containing the specified range from the original array, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Arrays; class Main { // Generic method to get subarray of a non-primitive array // between specified indices public static<T> T[] subArray(T[] array, int beg, int end) { return Arrays.copyOfRange(array, beg, end + 1); } public static void main(String[] args) { String[] arr = { "A", "B", "C", "D", "E", "F", "G", "H" }; int beg = 1, end = 4; String[] subarray = subArray(arr, beg, end); System.out.println(Arrays.toString(subarray)); } } |
Output:
[B, C, D, E]
2. Using Java 8
We can use the Java Stream, introduced in Java SE 8, to get a subarray from an array. The idea is to get a stream of elements between the specified range and then call the toArray() method to accumulate the stream elements into a new array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; import java.util.stream.IntStream; class Main { // Get subarray of a non-primitive array between specified indices public static void main(String[] args) { String[] arr = { "A", "B", "C", "D", "E", "F", "G", "H" }; int beg = 1, end = 4; String[] subarray = IntStream.range(beg, end + 1) .mapToObj(i -> arr[i]) .toArray(String[]::new); System.out.println(Arrays.toString(subarray)); } } |
Output:
[B, C, D, E]
3. Using System.arraycopy() method
System.arraycopy() method can also be used to get a copy from the specified position in the source array to the specified position of the destination array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; class Main { // Get subarray of a non-primitive array between specified indices public static void main(String[] args) { String[] arr = new String [] {"A", "B", "C", "D", "E", "F", "G", "H"}; int beg = 1, end = 4; String[] subarray = new String[end - beg + 1]; System.arraycopy(arr, beg, subarray, 0, subarray.length); System.out.println(Arrays.toString(subarray)); } } |
Output:
[B, C, D, E]
4. Converting to List
Here, the idea is to convert the array into a list and use the subList() method to get elements between the desired range. Then, we use the toArray(T[]) method to copy the list into a newly allocated array.
Please note that this method doesn’t actually copy the array. Both the resultant list and the sublist are backed by the original array and just references the array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; class Main { // Get subarray of a non-primitive array between specified indices public static void main(String[] args) { String[] arr = new String [] {"A", "B", "C", "D", "E", "F", "G", "H"}; int beg = 1, end = 4; String[] subarray = Arrays.asList(arr) .subList(beg, end + 1) .toArray(new String[0]); System.out.println(Arrays.toString(subarray)); } } |
Output:
[B, C, D, E]
5. Custom method
We can also write our own custom method, which simply copies the desired elements from the original array into the new array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; class Main { // Get subarray of a non-primitive array between specified indices public static void main(String[] args) { String[] arr = new String[] { "A", "B", "C", "D", "E", "F", "G", "H" }; int beg = 1, end = 4; String[] subarray = new String[end - beg + 1]; for (int i = 0; i < subarray.length; i++) { subarray[i] = arr[beg + i]; } System.out.println(Arrays.toString(subarray)); } } |
Output:
[B, C, D, E]
6. Using Apache Commons Lang
Apache Commons ArrayUtils class provides subarray() method that returns an Object subarray containing the elements between specified indices. See how to convert an object array to an Integer array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import org.apache.commons.lang3.ArrayUtils; import java.util.Arrays; class Main { // Get subarray of a non-primitive array between specified indices public static void main(String[] args) { String[] arr = new String[] { "A", "B", "C", "D", "E", "F", "G", "H" }; int beg = 1, end = 4; Object[] subarray = ArrayUtils.subarray(arr, beg, end + 1); System.out.println(Arrays.toString(subarray)); } } |
Output:
[B, C, D, E]
7. Using Arrays.copyOf() method
Finally, Arrays.copyOf() can also copy the specified array to an array of the specified type. But this approach won’t work if the subarray starts at some intermediate index rather than the first index.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; class Main { public static void main(String[] args) { String[] arr = new String [] {"A", "B", "C", "D", "E", "F", "G", "H"}; int end = 4; // beg = 0 String[] subarray = Arrays.copyOf(arr, end + 1, String[].class); System.out.println(Arrays.toString(subarray)); } } |
Output:
[A, B, C, D, E]
That’s all about getting a subarray of an array between specified indexes in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)