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
import java.util.Arrays;
class GFG {
public static int [] getSliceOfArray( int [] arr,
int start, int end)
{
int [] slice = new int [end - start];
for ( int i = 0 ; i < slice.length; i++) {
slice[i] = arr[start + i];
}
return slice;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int start = 2 , end = 4 ;
int [] slice = getSliceOfArray(arr, start, end + 1 );
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
import java.util.Arrays;
class GFG {
public static int [] getSliceOfArray( int [] arr,
int startIndex,
int endIndex)
{
int [] slice
= Arrays.copyOfRange(arr, startIndex, endIndex);
return slice;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int start = 2 , end = 4 ;
int [] slice = getSliceOfArray(arr, start, end + 1 );
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
import java.util.Arrays;
import java.util.stream.IntStream;
class GFG {
public static int [] getSliceOfArray( int [] arr,
int startIndex,
int endIndex)
{
int [] slice = IntStream
.range(startIndex, endIndex)
.map(i -> arr[i])
.toArray();
return slice;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int start = 2 , end = 4 ;
int [] slice = getSliceOfArray(arr, start, end + 1 );
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
import java.util.Arrays;
import java.util.stream.IntStream;
class GFG {
public static int [] getSliceOfArray( int [] arr,
int startIndex,
int len)
{
int [] slice = new int [len];
System.arraycopy(arr, startIndex, slice, 0 ,
len);
return slice;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int start = 2 , len = 3 ;
int [] slice = getSliceOfArray(arr, start, len);
System.out.println(Arrays.toString(slice));
}
}
|
- 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
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class GFG {
static int [] getSliceOfArray( int [] arr, int fromIndex, int toIndex){
List<Integer> ar = Arrays.stream(arr).boxed().collect(Collectors.toList());
List<Integer> arrl = ar.subList(fromIndex, toIndex);
int [] slice = arrl.stream().mapToInt(i -> i).toArray();
return slice;
}
public static void main (String[] args) {
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int start = 2 , end = 5 ;
int [] slice = getSliceOfArray(arr, start, end);
System.out.println(Arrays.toString(slice));
}
}
|
- 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
import java.util.Arrays;
import java.util.stream.Collectors;
class GFG {
static int [] getSliceOfArray( int [] arr, int startIndex,
int len)
{
int [] slice = Arrays.stream(arr)
.skip(startIndex)
.limit(len)
.toArray();
return slice;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int start = 2 , len = 3 ;
int [] slice = getSliceOfArray(arr, start, len);
System.out.println(Arrays.toString(slice));
}
}
|
- 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
import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
class GFG {
static int [] getSliceOfArray( int [] arr, int startIndex, int endIndex)
{
int [] slice = ArrayUtils.subarray(arr, startIndex, endIndex);
return slice;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int start = 2 , end = 5 ;
int [] slice = getSliceOfArray(arr, start, end);
System.out.println(Arrays.toString(slice));
}
}
|
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
import com.google.common.primitives.Ints;
import java.util.Arrays;
import java.util.List;
class GFG {
static int [] getSliceOfArray( int [] arr, int startIndex,
int length)
{
List<Integer> list = Ints.asList(arr);
List<Integer> subList
= list.subList(startIndex, startIndex + length);
int [] slice = Ints.toArray(subList);
return slice;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 3 , 4 , 5 };
int start = 2 , len = 3 ;
int [] slice = getSliceOfArray(arr, start, len);
System.out.println(Arrays.toString(slice));
}
}
|
Output
[3, 4, 5]
Share your thoughts in the comments
Please Login to comment...