forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ragged_Array.java
53 lines (51 loc) · 1.5 KB
/
Ragged_Array.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.*;
public class Ragged_Array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Number of Rows in the Ragged array: ");
int row =sc.nextInt();
//Asking for the number of rows
int [][] arr=new int [row][]; //u have to mention row number
/*
In case of Ragged Array or Zagged Array the number of elements that means
the number of columns are different for each row.
So, for this scenario at the time of Ragged Array declaration at first,
we have to define the number of total rows without defining the column
number.
Row number mentioning is mandatory.
*/
for(int i=0;i<row;i++)
{
System.out.print("Enter the number of Elements in row: "+(i+1)+": ");
arr[i]=new int[sc.nextInt()];
}
/*
Here, we have defined the number of elements for each row.
*/
// arr[0]= new int [4];
// arr[1]=new int[5];
// arr[2]=new int [2];
for (int i=0;i<arr.length;i++)
{
for(int j=0; j<arr[i].length;j++)
{
System.out.print("Enter the elements in row:"+(i+1)+": ");
arr[i][j]=sc.nextInt();
}
System.out.println();
}
/*
Here, we have initialized the Ragged Array after taking values from the user.
*/
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();
}
//This block has been used to print all the elements of the Ragged Array.
sc.close();
}
}