Question: Try creating the triangular array from the previous section into a simple class or in jshell. Heres one way: Now expand that code to print

Try creating the triangular array from the previous section into a simple class or in jshell. Here’s one way: 

int[][] triangle = new int[5][]; for (int i = 0; i < triangle.length; i++) { triangle[i] = new int [i + 1];

Now expand that code to print the contents of triangle to the screen. To help, recall that you can print the value of an array element with the System.out.println() method: 

System.out.println(triangle[3][1]);

Your output will probably be a long, vertical line of numbers like this: 

0
1
2
2
3
4
3
4

5
6
4
5
6
7
8

int[][] triangle = new int[5][]; for (int i = 0; i < triangle.length; i++) { triangle[i] = new int [i + 1]; for (int j = 0; j < i + 1; j++) triangle[i][j] = i + j; }

Step by Step Solution

3.36 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Heres how you can expand the given code to print the contents of the triangle array public class Tri... View full answer

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 Java An Introduction to Problem Solving and Progra Questions!