Get a slice of a primitive array in Java
This post will discuss how to get a slice of a primitive array in Java. In other words, get a subarray of a given primitive array between specified indexes.
1. Using Arrays.copyOfRange() method
The idea is to use the Arrays.copyOfRange() to copy the specified range of the specified array into a new array. Arrays class provides several overloaded versions of the copyOfRange() method for all primitive types.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.Arrays; class Main { // get a slice of a primitive array in Java public static void main(String[] args) { int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int start = 2, end = 5; int[] slice = Arrays.copyOfRange(arr, start, end + 1); System.out.println(Arrays.toString(slice)); } } |
Output:
[2, 3, 4, 5]
2. Using Java 8
From Java 8 onwards, we can simply get a stream of elements between the specified range and then call the toArray() method to accumulate the stream elements into a corresponding primitive 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 a slice of a primitive array in Java public static void main(String[] args) { int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int start = 2, end = 5; int[] slice = IntStream.range(start, end + 1) .map(i -> arr[i]) .toArray(); System.out.println(Arrays.toString(slice)); } } |
Output:
[2, 3, 4, 5]
3. Using Apache Commons Lang
Apache Commons provides the ArrayUtils class that has several utility methods for primitive arrays. It has a subarray() method that can produce a primitive subarray containing the elements between specified indexes.
|
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 a slice of a primitive array in Java public static void main(String[] args) { int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int start = 2, end = 5; int[] slice = ArrayUtils.subarray(arr, start, end + 1); System.out.println(Arrays.toString(slice)); } } |
Output:
[2, 3, 4, 5]
4. Naive solution
Finally, we can write our own custom method, which creates a new array and copy the elements from the original array to the new array by iterating over the array between specified indexes.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; class Main { // get a slice of a primitive array in Java public static void main(String[] args) { int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int start = 2, end = 5; int[] slice = new int[end - start + 1]; for (int i = 0; i < slice.length; i++) { slice[i] = arr[start + i]; } System.out.println(Arrays.toString(slice)); } } |
Output:
[2, 3, 4, 5]
That’s all about getting a slice of a primitive array 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 :)