LAST UPDATED: MARCH 5, 2021
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:
- Start
- Declare the size of the array.
- Initialize the number of rows of the jagged array.
- Now, initialize the arrays of the jagged array.
- First, declare the number of columns for the first row.
- Now, declare the number of columns for the second row.
- Declare a variable for the elements to be printed and initialize it to zero.
- Start initializing the array elements.
- Use two for loops for the same.
- Use the first for loop to iterate through the rows.
- Use the second for loop to iterate through the columns.
- Now, initialize the elements by incrementing the count element.
- Now, display the values of the 2D jagged array using two for loops.
- 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[2][];
// Initializing the arrays of jagged arrays
// First row has 4 columns
arr[0] = new int[4];
// Second row has 5 columns
arr[1] = new int[5];
// 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
- Start
- Declare the size of the array.
- Ask the user to initialize the number of rows for the jagged arrays.
- Ask the user to initialize the number of columns for each row for the jagged arrays.
- Use a for loop for the same.
- Start initializing the array elements.
- Use two for loops for the same.
- Use the first for loop to iterate through the rows.
- Use the second for loop to iterate through the columns.
- Now, initialize the elements by incrementing the count element.
- Now, display the values of the 2D jagged array using two for loops.
- 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