How to get slice of a Primitive Array in Java
Last Updated :
01 Mar, 2023
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
// Java program to Get a Slice
// of a Primitive Array
import java.util.Arrays;
class GFG {
// Function to get slice of a primitive array in Java
public static int[] getSliceOfArray(int[] arr,
int start, int end)
{
// Get the slice of the Array
int[] slice = new int[end - start];
// Copy elements of arr to slice
for (int i = 0; i < slice.length; i++) {
slice[i] = arr[start + i];
}
// return the slice
return slice;
}
public static void main(String[] args)
{
// Get the array, startIndex and endIndex
int[] arr = { 1, 2, 3, 4, 5 };
int start = 2, end = 4;
// Get the slice of the array
int[] slice = getSliceOfArray(arr, start, end + 1);
// Print the slice of the array
System.out.println(Arrays.toString(slice));
}
}
- 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
// Java program to Get a Slice
// of a Primitive Array
import java.util.Arrays;
class GFG {
// Function to get slice of a primitive array in Java
public static int[] getSliceOfArray(int[] arr,
int startIndex,
int endIndex)
{
// Get the slice of the Array
int[] slice
= Arrays.copyOfRange(arr, startIndex, endIndex);
// return the slice
return slice;
}
public static void main(String[] args)
{
// Get the array, startIndex and endIndex
int[] arr = { 1, 2, 3, 4, 5 };
int start = 2, end = 4;
// Get the slice of the array
int[] slice = getSliceOfArray(arr, start, end + 1);
// Print the slice of the array
System.out.println(Arrays.toString(slice));
}
}
- 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
// Java program to Get a Slice
// of a Primitive Array
import java.util.Arrays;
import java.util.stream.IntStream;
class GFG {
// Function to get slice of a primitive array in Java
public static int[] getSliceOfArray(int[] arr,
int startIndex,
int endIndex)
{
// Get the slice of the Array
int[] 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 slice
return slice;
}
public static void main(String[] args)
{
// Get the array, startIndex and endIndex
int[] arr = { 1, 2, 3, 4, 5 };
int start = 2, end = 4;
// Get the slice of the array
int[] slice = getSliceOfArray(arr, start, end + 1);
// Print the slice of the array
System.out.println(Arrays.toString(slice));
}
}
- Method 4: Using System.arraycopy() method.
- Get the Source Array, its startIndex and total no. of components to be copied
- Get the Destination Array and its startIndex
- Get the slice using System.arraycopy() method.
- Return or print the slice of the array.
Syntax:
public static void arraycopy(Object source_arr, int sourcePos, Object dest_arr, int destPos, int len)
Below is the implementation of the above approach:
Java
// Java program to Get a Slice
// of a Primitive Array
// Using System.arraycopy()
import java.util.Arrays;
import java.util.stream.IntStream;
class GFG {
// Function to get slice of a primitive array in Java
public static int[] getSliceOfArray(int[] arr,
int startIndex,
int len)
{
// Get the slice of the Array
int[] slice = new int[len];
// Slice the original array
System.arraycopy(arr, startIndex, slice, 0,
len); // [3, 4, 5]
// return the slice
return slice;
}
public static void main(String[] args)
{
// Get the array, startIndex and endIndex
int[] arr = { 1, 2, 3, 4, 5 };
int start = 2, len = 3;
// Get the slice of the array
int[] slice = getSliceOfArray(arr, start, len);
// Print the slice of the array
System.out.println(Arrays.toString(slice));
}
}
// This code is contributed by Susobhan Akhuli
- Method 5: Using List.subList() method.
- Get the Source Array, its startIndex and endIndex to be copied
- Convert the Source Array to ArrayList
- Get the slice using List.subList() method
- Again Convert the Sliced ArrayList to Sliced Array
- Return or print the slice of the array.
Syntax:
public List subList(int fromIndex, int toIndex)
Below is the implementation of the above approach:
Java
// Java program to Get a Slice
// of a Primitive Array
// Using List.subList()
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class GFG {
// Function to get slice of a primitive array in Java
static int[] getSliceOfArray(int[] arr, int fromIndex, int toIndex){
// Conversion of Array To ArrayList
List<Integer> ar = Arrays.stream(arr).boxed().collect(Collectors.toList());
// Get the slice of the Original Array
List<Integer> arrl = ar.subList(fromIndex, toIndex);
// Conversion of ArrayList to Array
int[] slice = arrl.stream().mapToInt(i -> i).toArray();
// return the slice
return slice;
}
public static void main (String[] args) {
// Get the array, startIndex and endIndex
int[] arr = { 1, 2, 3, 4, 5 };
int start = 2, end = 5;
// Get the slice of the array
int[] slice = getSliceOfArray(arr, start, end);
// Print the slice of the array
System.out.println(Arrays.toString(slice));
}
}
// This code is contributed by Susobhan AKhuli
- Method 6: Using Streams API:
- Get the Source Array, its startIndex and total no. of components to be copied
- Get the slice using stream method
- Return or print the slice of the array.
Syntax:
int[] sliced = Arrays.stream(original)
.skip(startIndex)
.limit(length)
.toArray();
Below is the implementation of the above approach:
Java
// Java program to Get a Slice
// of a Primitive Array
// Using Stream API
import java.util.Arrays;
import java.util.stream.Collectors;
class GFG {
// Function to get slice of a primitive array in Java
static int[] getSliceOfArray(int[] arr, int startIndex,
int len)
{
// Get the slice of the array
int[] slice = Arrays.stream(arr)
.skip(startIndex)
.limit(len)
.toArray();
// return the slice
return slice;
}
public static void main(String[] args)
{
// Get the array, startIndex and endIndex
int[] arr = { 1, 2, 3, 4, 5 };
int start = 2, len = 3;
// Get the slice of the array
int[] slice = getSliceOfArray(arr, start, len);
// Print the slice of the array
System.out.println(Arrays.toString(slice));
}
}
// This code is contributed by Susobhan AKhuli
- Method 7: Using ArrayUtils.subarray() Method:
- Get the Source Array, its startIndex and endIndex to be copied
- Get the slice using ArrayUtils.subarray() method
- Return or print the slice of the array.
Syntax:
int[] sliced = ArrayUtils.subarray(Original_array, Start_Index, End_Index);
Below is the implementation of the above approach:
Java
// Java program to Get a Slice
// of a Primitive Array
// Using ArrayUtils.subarray() Method
import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
class GFG {
// Function to get slice of a primitive array in Java
static int[] getSliceOfArray(int[] arr, int startIndex, int endIndex)
{
// Get the slice of the array
int[] slice = ArrayUtils.subarray(arr, startIndex, endIndex);
// return the slice
return slice;
}
public static void main(String[] args)
{
// Get the array, startIndex and endIndex
int[] arr = { 1, 2, 3, 4, 5 };
int start = 2, end = 5;
// Get the slice of the array
int[] slice = getSliceOfArray(arr, start, end);
// Print the slice of the array
System.out.println(Arrays.toString(slice));
}
}
// This code is contributed by Susobhan AKhuli
Output
[3, 4, 5]
- Method 8: Using Guava library:
- Get the Source Array, its startIndex and length to be copied
- Convert array to list
- Get the slice using Guava library
- convert list to array again
- Return or print the slice of the array.
Syntax:
List<Integer> subList = list.subList(startIndex, startIndex + length);
Below is the implementation of the above approach:
Java
// Java program to Get a Slice
// of a Primitive Array
// Using Guava library
import com.google.common.primitives.Ints;
import java.util.Arrays;
import java.util.List;
class GFG {
// Function to get slice of a primitive array in Java
static int[] getSliceOfArray(int[] arr, int startIndex,
int length)
{
// convert array to list
List<Integer> list = Ints.asList(arr);
// Get the slice of the list
List<Integer> subList
= list.subList(startIndex, startIndex + length);
// convert list to array
int[] slice = Ints.toArray(subList);
// return the slice
return slice;
}
public static void main(String[] args)
{
// Get the array, startIndex and length of sliced
// array
int[] arr = { 1, 2, 3, 4, 5 };
int start = 2, len = 3;
// Get the slice of the array
int[] slice = getSliceOfArray(arr, start, len);
// Print the slice of the array
System.out.println(Arrays.toString(slice));
}
}
// This code is contributed by Susobhan AKhuli
Output
[3, 4, 5]
Similar Reads
Simplest and Best method to print a 2D Array in Java Given a 2d array arr in Java, the task is to print the contents of this 2d array. Method 1: Loop method The first thing that comes to mind is to write a nested for loop, and print each element by arr[i][j]. Java // Java program to print 2d array // using Loop method import java.io.*; import java.uti
2 min read
Array getShort() method in Java The java.lang.reflect.Array.getShort() is an in-built method of Array class in Java and is used to return the element present at a given index from the specified Array as a short. Syntax: Array.getShort(Object []array,int index) Parameters: array: The object array whose index is to be returned. inde
3 min read
Array get() Method in Java The java.lang.reflect.Array.get() is an inbuilt method in Java and is used to return the element at a given index from the specified Array. Syntax Array.get(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose index is to be returned. in
3 min read
Find max or min value in an array of primitives using Java A simple method is traverse the array. We initialize min and max as first elements and then compare every element (Starting from 2nd) with current min and max.Java// Java program to find min & max in int[] // array without asList() public class MinNMax { public static void main(String[] args) {
3 min read
Array getLong() Method in Java The java.lang.reflect.Array.getLong() is an inbuilt method in Java and is used to return an element at the given index from a specified Array as a long. Syntax: Array.getLong(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose index is
3 min read
How to Get Last Element in Array in Java? In Java, to get the last element in an array, we can access the element at the index array.length - 1 using array indexing. The length property of the array provides its total size, and subtracting one from it gives the index of the last element.Example 1: Here, we will access the last element in an
2 min read
Array getInt() Method in Java The java.lang.reflect.Array.getInt() is an inbuilt method in Java and is used to return an element at the given index from the specified Array as a int. Syntax Array.getInt(Object []array, int index) Parameters: This method accepts two mandatory parameters: array: The object array whose index is to
3 min read
Array getBoolean() Method in Java The java.lang.reflect.Array.getBoolean() returns the given index from the specified Array as a short. Syntax: Array.getBoolean(Object []array,int index) Parameters: array: The object array whose index is to be returned. index: The particular index of the given array. The element at 'index' in the gi
3 min read
Array getDouble() Method in Java The java.lang.reflect.Array.getDouble() is an inbuilt method of Array class in Java and is used to return the element present at the given index from the specified Array as Double. Syntax: Array.getDouble(Object []array, int index) Parameters: This method accepts two mandatory parameters: array: The
3 min read
Array getByte() Method in Java The java.lang.reflect.Array.getByte() is an inbuilt method in Java and is used to return the element present at the given index from the specified Array as a Byte.Syntax: Array.getByte(Object []array, int index) Parameters : This method accepts two mandatory parameters: array: The object array whose
3 min read