How to get slice of a Primitive Array in Java

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.
    1. Get the Array and the startIndex and the endIndex.
    2. Create and empty primitive array of size endIndex-startIndex.
    3. Copy the elements from startIndex to endIndex from the original array to the slice array.
    4. Return or print the slice of the array.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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));
        }
    }

    chevron_right

    
    

    Output:

    [3, 4, 5]
    
  • Method 2: Using Arrays.copyOfRange() method.
    1. Get the Array and the startIndex and the endIndex.
    2. Get the slice using Arrays.copyOfRange() method.
    3. Return or print the slice of the array.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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));
        }
    }

    chevron_right

    
    

    Output:

    [3, 4, 5]
    
  • Method 3: Using Java 8 Streams
    1. Get the Array and the startIndex and the endIndex.
    2. Convert the specified range of elements from the startIndex to endIndex to Primitive Stream using range() method.
    3. Map the specified elements from the original array using map() method.
    4. Convert the mapped array into array using toArray() method.
    5. Return or print the slice of the array.

    Below is the implementation of the above approach:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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));
        }
    }

    chevron_right

    
    

    Output:

    [3, 4, 5]
    


My Personal Notes arrow_drop_up

Technical Content Engineer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.