Java Program To Print All Subarrays of a Given Array

In the previous article, we have discussed about Java Program to Move An Array Element From One Array Position to Another Position

In this article we are going to see how we can print all the subarrays of an array by using Java programming language.

Java Program To Print All Subarrays of a Given Array

A continuous portion of an array is called as sub array.

Example:

Array is {1,2,3}

Then the subarrays would be {1},{2},{3},{1,2},{2,3},{1,2,3}

Let’s see different ways to print all the subarrays of an array.

Method-1: Java Program To Print All Subarrays of a Given Array By Using Recursion

In this method we will use iteration to print the subarrays.

Approach:

  • Create a new array with some elements in it.
  • Pass the array into the subArray( ) function with initial start and end value as 0.
  • subArray( ) function is a recursive function that takes all elements of the array and iterates the array from first and last. It increments and decrements the index and then calls itself on the new values until we get all our sub arrays.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Arrays
        int arr[] = {1,2,3,4};
        System.out.println("The subarrays of "+Arrays.toString(arr)+" is-");
        // passes the array to the function
        // initial start and end value is zero
        subarray(arr,0,0);
    }

    public static void subarray(int arr[], int start, int end)
    {
    // Stop if we have reached the end of the array    
    if (end == arr.length)
        return;
    // It increases the end point and starts from the first element
    else if (start > end)
        subarray(arr, 0, end + 1);
    
    // Prints the subarray 
    else
    {
        for (int a = start; a < end; a++)
        {
            System.out.print(arr[a]+" ");
        }
        System.out.println(arr[end]);
        subarray(arr, start + 1, end);
    }
    return;

    }
}

Output:

The subarrays of [1, 2, 3, 4] is-
1
1 2
2
1 2 3
2 3
3
1 2 3 4
2 3 4
3 4
4

Method-2: Java Program To Print All Subarrays of a Given Array By Using For Loop

Approach:

  • Create a new array with some elements in it
  • Pass the array into the subArray( ) function.
  • subArray( ) function is a function that takes all elements of the array and iterates the array from first and last. Then prints the array.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Arrays
        int arr[] = {1,2,3,4};
        System.out.print("The subarrays of "+Arrays.toString(arr)+" is-");
        // passes the array to the function
        subArray(arr);
    }

    public static void subArray(int arr[])
    {
        // Stores the arraylength
        int n=arr.length;
        //This loop will select the first element and iterate forwards
        for(int i=0;i<=n;i++)
        {
            //This loop will select the last element and iterate backwards
            for(int j = i;j<=n;j++)
            {
                // Loop that prints the elements
                for(int k =i;k<j;k++)
                {
                    System.out.print( arr[k]+" "); 
                }
                System.out.println();
            }
        }
    }
}
Output:

The subarrays of [1, 2, 3, 4] is-
1 
1 2 
1 2 3 
1 2 3 4

2 
2 3 
2 3 4

3 
3 4

4

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: