Java get subarray – Java Program to Find a Subarray of an Array Between Specified Indexes

Java get subarray: In the previous article we have discussed about Java Program to Print all SubArrays of a Given Array

In this article we are going to see how we can find subarray of an array between specified index by using Java programming language.

Java Program to Find a Subarray of an Array Between Specified Indexes

Java subarray: Let’s see different ways to find subarray of an array between specified index.

Method-1: Java Program to Find a Subarray of an Array Between Specified Indexes By Using Arrays.copyOfRange() Method

Approach:

  • Take the array elements as input from the user.
  • Store the elements in an array.
  • Print the array to the user.
  • Ask the user to enter the beginning and ending index and store the elements.
  • Create another array and store the elements formed using the copyOfRange() library function.
  • Print the subarray.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args) 
    {
        // Scanner to take Input
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter array elements");
        // Takes the input as a single string separated by space 
        // and then splits it into elements of array
        String s = sc.nextLine();
        String str[] = s.split(" ");
        // Prints the array elements
        System.out.println("Array-"+Arrays.toString(str));
        // Asks the user to enter the start and end index of the subarray
        System.out.println("Enter the beginning and ending index-");
        int start = sc.nextInt(), end = sc.nextInt();
        // Stores the subarray formed using the copy range library fucntion
        String[] subArr = Arrays.copyOfRange(str,start,end+1);
        System.out.println("SubArray-"+Arrays.toString(subArr));
    }
}

Output:

Enter array elements
a b c d e f 
Array-[a, b, c, d, e, f]
Enter the beginning and ending index-
2
4
SubArray-[c, d, e]

Method-2: Java Program to Find a Subarray of an Array Between Specified Indexes By Using Java Stream API

Approach:

  • Take the array elements as input from the user.
  • Store the elements in an array.
  • Print the array to the user.
  • Ask the user to enter the beginning and ending index and store the elements.
  • Create another array and store the elements formed from the stream function.
  • Print the subarray.

Program:

import java.util.*;
import java.util.stream.IntStream;
public class Main
{
    public static void main(String[] args) 
    {
        // Scanner to take Input
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter array elements");
        // Takes the input as a single line and then splits it into elements
        String s = sc.nextLine();
        String str[] = s.split(" ");
        // Prints the array elements
        System.out.println("Array-"+Arrays.toString(str));
        // Asks the user to enter the start and end index of the subarray
        System.out.print("Enter the beginning and ending index-");
        int start = sc.nextInt(), end = sc.nextInt();
        // Stores the subarray formed using the JAVA stream API
        String[] subArr = IntStream.range(start, end + 1)
                                .mapToObj(i -> str[i])
                                .toArray(String[]::new);
        System.out.println("SubArray-"+Arrays.toString(subArr));
    }
}
Output:

Enter array elements
ab bc cd ef gh ij kl mn
Array-[ab, bc, cd, ef, gh, ij, kl, mn]
Enter the beginning and ending index-3 5
SubArray-[ef, gh, ij]

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: