Given a primitive array, the task is to get a slice of the array using the start and end index in Java.
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.
Below is the implementation of the above approach:
// 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)); } }
Output:
[3, 4, 5]
Method 2: Using Arrays.copyOfRange() method.
Below is the implementation of the above approach:
// 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( // Source arr, // The Start index startIndex, // The end index 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)); } }
Output:
[3, 4, 5]
Method 3: Using Java 8 Streams
Below is the implementation of the above approach:
// 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)); } }
Output:
[3, 4, 5]
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.