How to get slice of a Primitive Array in Java
Given a Primitive Array, the task is to get a Slice of this array in Java, using start and ending index.
Examples:
Input: arr[] = {1, 2, 3, 4, 5}, startIndex = 2, endIndex = 4
Output: {3, 4, 5}
Input: arr[] = {1, 2, 3, 4, 5}, startIndex = 0, endIndex = 1
Output: {1, 2}
- Method 1: Naive Method.
- Get the Array and the startIndex and the endIndex.
- Create and empty primitive array of size endIndex-startIndex.
- Copy the elements from startIndex to endIndex from the original array to the slice array.
- Return or print the slice of the array.
Below is the implementation of the above approach:
// Java program to Get a Slice// of a Primitive Arrayimportjava.util.Arrays;classGFG {// Function to get slice of a primitive array in Javapublicstaticint[] getSliceOfArray(int[] arr,intstart,intend){// Get the slice of the Arrayint[] slice =newint[end - start];// Copy elements of arr to slicefor(inti =0; i < slice.length; i++) {slice[i] = arr[start + i];}// return the slicereturnslice;}publicstaticvoidmain(String[] args){// Get the array, startIndex and endIndexint[] arr = {1,2,3,4,5};intstart =2, end =4;// Get the slice of the arrayint[] slice = getSliceOfArray(arr, start, end +1);// Print the slice of the arraySystem.out.println(Arrays.toString(slice));}}chevron_rightfilter_noneOutput:[3, 4, 5]
- Method 2: Using Arrays.copyOfRange() method.
- Get the Array and the startIndex and the endIndex.
- Get the slice using Arrays.copyOfRange() method.
- Return or print the slice of the array.
Below is the implementation of the above approach:
// Java program to Get a Slice// of a Primitive Arrayimportjava.util.Arrays;classGFG {// Function to get slice of a primitive array in Javapublicstaticint[] getSliceOfArray(int[] arr,intstartIndex,intendIndex){// Get the slice of the Arrayint[] slice = Arrays.copyOfRange(// Sourcearr,// The Start indexstartIndex,// The end indexendIndex);// return the slicereturnslice;}publicstaticvoidmain(String[] args){// Get the array, startIndex and endIndexint[] arr = {1,2,3,4,5};intstart =2, end =4;// Get the slice of the arrayint[] slice = getSliceOfArray(arr, start, end +1);// Print the slice of the arraySystem.out.println(Arrays.toString(slice));}}chevron_rightfilter_noneOutput:[3, 4, 5]
- Method 3: Using Java 8 Streams
- Get the Array and the startIndex and the endIndex.
- Convert the specified range of elements from the startIndex to endIndex to Primitive Stream using range() method.
- Map the specified elements from the original array using map() method.
- Convert the mapped array into array using toArray() method.
- Return or print the slice of the array.
Below is the implementation of the above approach:
// Java program to Get a Slice// of a Primitive Arrayimportjava.util.Arrays;importjava.util.stream.IntStream;classGFG {// Function to get slice of a primitive array in Javapublicstaticint[] getSliceOfArray(int[] arr,intstartIndex,intendIndex){// Get the slice of the Arrayint[] slice = IntStream// Convert the specified elements// of array into IntStream.range(startIndex, endIndex)// Lambda expression to get// the elements of IntStream.map(i -> arr[i])// Convert the mapped elements// into the slice array.toArray();// return the slicereturnslice;}publicstaticvoidmain(String[] args){// Get the array, startIndex and endIndexint[] arr = {1,2,3,4,5};intstart =2, end =4;// Get the slice of the arrayint[] slice = getSliceOfArray(arr, start, end +1);// Print the slice of the arraySystem.out.println(Arrays.toString(slice));}}chevron_rightfilter_noneOutput:[3, 4, 5]
Recommended Posts:
- Is an array a primitive type or an object in Java?
- Program to convert Primitive Array to Stream in Java
- Widening Primitive Conversion in Java
- Primitive Wrapper Classes are Immutable in Java
- Comparision of double and float primitive types in Java
- How to get Slice of a Stream in Java
- LongBuffer slice() method in Java
- IntBuffer slice() method in Java
- CharBuffer slice() method in Java
- ShortBuffer slice() method in Java with Examples
- ByteBuffer slice() method in Java with Examples
- FloatBuffer slice() method in Java with Examples
- DoubleBuffer slice() method in Java with Examples
- Set to Array in Java
- Array Copy in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



