如何在Java中获取原始数组的切片
给定一个原始数组,任务是使用开始和结束索引在Java中获取该数组的切片。
例子:
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}
- 方法1 :朴素方法。
- 获取数组以及 startIndex 和 endIndex。
- 创建并清空大小为 endIndex-startIndex 的原始数组。
- 将原始数组中从 startIndex 到 endIndex 的元素复制到切片数组。
- 返回或打印数组的切片。
下面是上述方法的实现:
// 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)); } }
输出:[3, 4, 5]
- 方法 2 :使用 Arrays.copyOfRange() 方法。
- 获取数组以及 startIndex 和 endIndex。
- 使用 Arrays.copyOfRange() 方法获取切片。
- 返回或打印数组的切片。
下面是上述方法的实现:
// 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)); } }
输出:[3, 4, 5]
- 方法 3 :使用Java 8 流
- 获取数组以及 startIndex 和 endIndex。
- 使用 range() 方法将指定范围的元素从 startIndex 转换为 endIndex 到 Primitive Stream。
- 使用 map() 方法映射原始数组中的指定元素。
- 使用 toArray() 方法将映射数组转换为数组。
- 返回或打印数组的切片。
下面是上述方法的实现:
// 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)); } }
输出:[3, 4, 5]