Jagged array java – Java Program to Create a Jagged Array that Contains Two Arrays

Jagged array java: In the previous article we have discussed about Java Program to Find a SubArray of an Array Between Specified Indexes

In this article, we are going to see how we can create a jagged array from two arrays by using Java programming language.

Java Program to Create a Jagged Array that Contains Two Arrays

Jagged arrays are arrays consisting of arrays as elements.

Let’s see different ways to create a jagged array from two arrays.

Method-1: Java Program to Create a Jagged Array that Contains Two Arrays with Predefined Inputs

Approach:

  • Create a jagged array with 2 rows.
  • Assign size to the array columns.
  • Use two for loops to assign elements to the arrays
  • Use two more for loops to print the array elements.

Program:

import java.util.*;
import java.util.Arrays;

public class Main
{
    public static void main(String[] args) 
    {
        // JaggedArray was created containing 2 rows
        int jaggedArr[][] = new int[2][];
        // The size of the arrays inside the jagged array is declared
        jaggedArr[0] = new int[5];
        jaggedArr[1] = new int[4];
        // Count is used to add elements into the jagged array
        int count = 0;
        for(int i = 0; i<2;i++)
        {
            for(int j = 0;j<jaggedArr[i].length;j++)
            {
                jaggedArr[i][j] = count++;
            }
        }
        
        // The output array is printed
        System.out.println("The jagged array is-");
        for (int i = 0; i < jaggedArr.length; i++)
        {
            for (int j = 0; j < jaggedArr[i].length; j++)
            {
                System.out.print(jaggedArr[i][j] + " ");
            }
            System.out.println();
        }
    }
}
Output:

The jagged array is-
0 1 2 3 4 
5 6 7 8

Method-2: Java Program to Create a Jagged Array that Contains Two Arrays with User Defined Inputs

Approach:

  • Ask the user to enter the size of both arrays.
  • Create a jagged array with 2 rows
  • Assign the lengths to the two 1D arrays under jagged array
  • Ask the user to enter the array elements and take input using two for loops.
  • Finally print the jagged array using two for loops.

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 the size of both arrays- ");
        int r = 2, c1 = sc.nextInt(), c2 = sc.nextInt();
        // Create Jagged Array
        int jaggedArray[][] = new int[2][];
        jaggedArray[0] = new int[c1];
        jaggedArray[1] = new int[c2];
        // Enter the elements
        System.out.println("Enter the elements");
        for (int i = 0; i < jaggedArray.length; i++)
        {
            for (int j = 0; j < jaggedArray[i].length; j++)
            {
                jaggedArray[i][j] = sc.nextInt();
            }
        }
        // Jagged Array Output
        System.out.println("Jagged Array-");
        for (int i = 0; i < jaggedArray.length; i++)
        {
            for (int j = 0; j < jaggedArray[i].length; j++)
            {
                System.out.print(jaggedArray[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Output:

Enter the size of both arrays- 
4
3
Enter the elements
1 2 3 4
5 6 7
Jagged Array-
1 2 3 4 
5 6 7

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: