Question: Java Ragged Arrays: The following code defines a ragged two-dimensional array. The term ragged indicates that the rows are not all the same length. int[][]

Java Ragged Arrays: The following code defines a ragged two-dimensional array. The term ragged indicates that the rows are not all the same length.

int[][] triangle new int[5][]; // allocate array of rows

for (int i 0; i triangle.length; i)

triangle[i] new int[r 1];

a. Draw a picture of the ragged array created by this code fragment.

b. Write a method int [][] buildRagged(int n) that returns a (possibly) ragged array with n rows. The method reads n+ 1 lines of data from the console. The first line contains the number of rows of a ragged array. Each succeeding line specifi es one row of a ragged array. The first entry of each line gives the number of items in that row. For example, the input

3

3 1 5 7

6 5 6 8 9 3 2

2 5 8

indicates that there are 3 rows and that the fi rst row of the ragged array has 3 entries (1, 5, and 7), the second row 6 entries (5, 6, 8, 9, 3, and 2), and the third row 2 entries (5 and 8). c. Write a method void printArray(int [][] x) that displays a (possibly) ragged array. d. Test your methods in a program using the main(...) method: public static void main( String[] args) { Scanner input new Scanner(System.in); System.out.print("Enter the data for the array, begin with the number of rows: "); int rows nextInt(); int[][] array build(rows); printArray(array); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!