-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIrregularArray.java
More file actions
40 lines (36 loc) · 1.14 KB
/
IrregularArray.java
File metadata and controls
40 lines (36 loc) · 1.14 KB
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
//Program to read and print an array of size n rows with variable column size.
import java.util.Scanner;
class IrregularArray
{
public static void main(String[] args)
{
System.out.print("\nEnter the number of rows: ");
Scanner reader = new Scanner(System.in);
int r = reader.nextInt();
// Declaring 2-D array with r rows
int arr[][] = new int[r][];
// Creating a 2D array such that first row
// has 1 element, second row has two
// elements and so on.
int col [] = new int[r];
for (int i = 0; i < arr.length; i++)
{
System.out.print("\nEnter the number of elements in row "+ (i+1 ));
col[i] = reader.nextInt();
arr[i] = new int[col[i]];
}
// Initializing array
int count = 0;
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[i].length; j++)
arr[i][j] = count++;
// Displaying the values of 2D Jagged array
System.out.println("Contents 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();
}
}
}