Array Slicing in Java
Array slicing is a method in Java for obtaining a subarray of a specified array. Assume a[] is an array. It contains eight items indexed from a[0] to a[7].a[] = {1, 5, 3, 2, 7, 12, 4, 11}
Now we need to locate a slice of a array index between a[4] and a[7]. Where a[4] represents the startIndex and a[7] represents the endIndex. As a result, we have the below sliced array:a[] = {7, 12, 4, 11}
In this section, we'll look at how to use Java to find a slice of an array. There are three methods for locating a slice of an array:
- By duplicating elements
- Using the copyOfRange() Method
- Using Java 8 Stream
Let's go over each method in depth:
Elements Copying Method:
It is a native technique for obtaining an array slice. In this technique, we first determine the beginning and ending indexes of the provided array. Then we make an empty array (sliced array) of size (endIndex - startIndex). Copies the elements (from startIndex) from the provided array to the sliced array. Finally, output the sliced array. Let's put the aforesaid method into action in a Java programme to get a sliced array of the given array. This is a program. An array of primitive types will be used.
SliceArrayExample:
import java.util.Arrays;
public class SliceArrExample
{
public static int[] getSlice(int[] arr, int a, int b)
{
int[] slicedArr= new int[b - a];
for (int i = 0; i < slicedArr.len; i++)
{
slicedArr[i] = array[a + i];
}
return slicedArr;
}
public static void main(String args[])
{
int[] arr= {1, 5, 8, 2, 4, 9, 6, 11, 0, 3};
int a= 4, b = 7;
int[] slicedArr= getSlice(arr, a, b + 1);
System.out.println("Slice of Arr: "+Arrays.toStr(slicedArr));
}
}
Output:
Slice of Arr: [4, 9, 6, 11]
Using the copyOfRange() Function:
The copyOfRange() method is a member of the Java Arrays class. It replicates the array's specified range to the newly formed array (slice array) and provides the newly produced array containing the array's specified range from the original array. The slicing of an array takes O(n) time and O(n) space to contain elements, where n represents the number of elements in the new array.
Syntax:
public static int[] copyOfRange(int[] original, int from, int to)
The method parses the following three parameters:
- original: It is an array whose slice must be discovered.
- It is the starting index. It must be in the range of 0 to the length of the provided array.
- It is the final index.
The following exceptions are thrown:
- If from is less than 0 or higher than the length of the provided array, an ArrayIndexOutOfBoundsException is thrown.
- If the parameter from is bigger than the parameter to, an IllegalArgumentException is thrown.
- If the provided array is null, a NullPointerException is thrown.
SliceArrayExample1:
import java.util.Arrays;
public class SliceArrExample1
{
public static int[] slice(int[] arr, int a, int b)
{
int[] slicedArr = Arrays.copyOfRange(arr, a, b);
return slicedArr;
}
public static void main(String args[])
{
//getting array, a and b
int[] arr= {1, 2, 6, 9, 11, 8, 5, 4, 3};
int a= 1, b = 5;
int[] sliceArr= slice(arr, a, b+ 1);
System.out.println("Slice of Arr: "+Arrays.toStr(sliceArr));
}
}
Output:
Slice of Arr: [2, 6, 9,11, 8]
Using the Java 8 Stream:
We may use the Java 8 Stream to find the slicing of an array by following the instructions below.
- Find the startIndex and endIndex arrays first.
- Using the range() technique, convert the elements in range into Primitive Stream.
- Map the requested elements from the supplied array using the map() function.
- Convert the mapped array to an array by calling the toArray() function.
- Please print the sliced.
SliceArrayExample2:
import java.util.Arrays;
import java.util.stream.IntStream;
public class SliceArrayExample2
{
public static int[] findSl(int[] array, int startIndex, int endIndex)
{
int[] slcarray = IntStream.range(a, e).map(i -> array[i]).toArr();
return slcarray;
}
//main() method
public static void main(String args[])
{
//Get the array, a and b
int[] array = {2, 5, 9, 5, 4, 10, 3, 8, 7, 12, 0, 9};
int a= 4, b = 8;
int[] slcarr = findS(array, a, b + 1);
//Printing the slice of the array
System.out.println("Array for the specified range is: "+Arrays.toStr(slcarr));
}
}
Output:
Array for the specified range is: [4,10, 3, 8, 7]