Dark Mode On/Off

# Java Program To Create a Jagged Array that Contains Two Arrays

In this tutorial, we will learn how to create a jagged array that contains two arrays. A Jagged Array is defined as an array where each element of that array is an array itself. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java. Input:

Enter the number of rows for a jagged array: 2

Enter the number of columns for each row : 3 4

Enter the elements: 1 2 3 4 5 6 7

Output:

Elements of the array:

1 2 3

4 5 6 7

## Program 1: Create a Jagged Array

In this method, we will see how to create a jagged array which contains two 1D arrays with pre-defined inputs.

### Algorithm:

1. Start
2. Declare the size of the array.
3. Initialize the number of rows of the jagged array.
4. Now, initialize the arrays of the jagged array.
5. First, declare the number of columns for the first row.
6. Now, declare the number of columns for the second row.
7. Declare a variable for the elements to be printed and initialize it to zero.
8. Start initializing the array elements.
9. Use two for loops for the same.
10. Use the first for loop to iterate through the rows.
11. Use the second for loop to iterate through the columns.
12. Now, initialize the elements by incrementing the count element.
13. Now, display the values of the 2D jagged array using two for loops.
14. Stop

Below is the code for the same.

The below program demonstrates how to create a jagged array that contains two arrays with pre-defined inputs

``````/*Java Program to to create a jagged array which contains two 1D array*/

public class Main
{
public static void main(String[] args)
{
// Declaring 2-D array with 2 rows
int arr[][] = new int[];

// Initializing the arrays of jagged arrays

// First row has 4 columns
arr = new int;

// Second row has 5 columns
arr = new int;

// Initializing array
int count = 0;
for (int i = 0; i < arr.length; i++)   //For Rows
{
for (int j = 0; j < arr[i].length; j++)   //For Columns
{
arr[i][j] = count++;
}
}

// Displaying the values of 2D Jagged array
System.out.println("Elements of 2D Jagged Array");
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}``````

Elements of 2D Jagged Array
0 1 2 3
4 5 6 7 8

## Program 2: Create a Jagged Array

In this method, we will see how to create a jagged array that contains two 1D arrays with user-defined inputs.

### Algorithm

1. Start
2. Declare the size of the array.
3. Ask the user to initialize the number of rows for the jagged arrays.
4. Ask the user to initialize the number of columns for each row for the jagged arrays.
5. Use a for loop for the same.
6. Start initializing the array elements.
7. Use two for loops for the same.
8. Use the first for loop to iterate through the rows.
9. Use the second for loop to iterate through the columns.
10. Now, initialize the elements by incrementing the count element.
11. Now, display the values of the 2D jagged array using two for loops.
12. Stop

Below is the code for the same.

The below program demonstrates how to create a jagged array that contains two 1D arrays with user-defined inputs.

``````/*Java Program to to create a jagged array which contains two 1D array*/
import java.util.*;

public class Main
{
public static void main(String[] args)
{
//Taking Input from the user
Scanner sc=new Scanner(System.in);

//Declare number of rows for jagged array
int m;
System.out.println("Enter the number of rows for jagged arrays");
m=sc.nextInt();     //Initialize the number of rows for jagged array

// Declaring 2-D array with m rows
int arr[][] = new int[m][];

//Initializing the columns for each rows of jagged array
System.out.println("Enter the number of columns for each rows of jagged arrays");
for(int i=0;i<m;i++)
{
arr[i]=new int[sc.nextInt()];
}

// Initializing array
System.out.println("Enter the elements");
for (int i = 0; i < arr.length; i++)   //For Rows
{
for (int j = 0; j < arr[i].length; j++)   //For Columns
{
arr[i][j] = sc.nextInt();
}
}

// Displaying the values of 2D Jagged array
System.out.println("Elements of 2D Jagged Array");
for (int i = 0; i < arr.length; i++)     //For Rows
{
for (int j = 0; j < arr[i].length; j++)    //For Columns
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}``````

Enter the number of rows for jagged arrays 2
Enter the number of columns for each rows of jagged arrays 3 4
Enter the elements 1 2 3 4 5 6 7
Elements of 2D Jagged Array
1 2 3
4 5 6 7