0% found this document useful (0 votes)
5 views2 pages

Creating a Jagged Array in Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Creating a Jagged Array in Java

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

Scanner;

public class JaggedArrayScanner {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

// Step 1: Ask user how many rows


[Link]("Enter number of rows: ");
int rows = [Link]();

// Step 2: Create jagged array (only rows fixed for now)


int[][] arr = new int[rows][];

// Step 3: For each row, ask how many columns and take
input
for (int i = 0; i < rows; i++) {
[Link]("Enter number of columns for row " +
(i + 1) + ": ");
int cols = [Link]();
arr[i] = new int[cols]; // allocate memory for
columns

// Take input for each element


[Link]("Enter " + cols + " elements for
row " + (i + 1) + ": ");
for (int j = 0; j < cols; j++) {
arr[i][j] = [Link]();
}
}

// Step 4: Print jagged array


[Link]("\nJagged Array Output:");
for (int i = 0; i < [Link]; i++) {
for (int j = 0; j < arr[i].length; j++) {
[Link](arr[i][j] + " ");
}
[Link]();
}
}
}

You might also like