问题
I'm trying to write a program for an assignment. The requirements are to create Pascal's Triangle recursively and then print a given line. However, after compiling my program, I get several ArrayIndexOutOfBoundsExceptions. Here is the stack trace: java.lang.ArrayIndexOutOfBoundsException: 10 at pasTriangle.populateT(pasTriangle.java:79) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:93) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java: 93) at pasTriangle.populateT(pasTriangle.java:86) at pasTriangle.populateT(pasTriangle.java:86)at pasTriangle.populateT(pasTriangle.java:86)at pasTriangle.populateT(pasTriangle.java:86)at pasTriangle.populateT(pasTriangle.java:86)
Does anyone know what I'm doing wrong? I've tried everything, esp changing condiionals but nothing works. Here is my code:
public class pasTriangle
{
private int size, row, col; //Represents the number of lines the triangle has.
private int [][] pTriangle; //2-D array to hold int values of triangle
/* ****************************************************************************************************************************************************
Constructor creates a 2D array to hold the Pascales triangle. Note the number of numbers on each line is the same as the number of lines in
the triangle, so size can be used for both values. Calls populateT method to populate the triangle.
***************************************************************************************************************************************************/
public pasTriangle(int size)
{
this.size = size;
pTriangle = new int[size][size];
}
/* ****************************************************************************************************************************************************
Method which populates the Pascal's Triangle recursively. Note case where size = 1, recursion does not occur since only 1 integer can be added
to array.
Also note base case where base of triangle is reached and recursion stops.
Also note cases for first and last value of each line (row).
Appropriate values added to each index according to conditions.
*********************************************************************************************************************************************************/
public void populateT(int row, int col)
{
if(size > 0 && size == 1 && row < size)
{
pTriangle[0][0] = 1;
}
else if(size > 1 && row < size)
{
if (col==0 && row == 0) //First value.
{
pTriangle[row][col] = 1;
}
else if (row != 0 && col == 0 || col == pTriangle[row].length-1)
{
pTriangle [row][col] = 1; //1 Set for first value in each line and last value in each line.
}
else if(row > 1 && col != 0 && col != pTriangle[row].length-1) //Values in between first and last calculated from the two above them, to left and right.
{
pTriangle[row][col] = (pTriangle[row-1][col-1]) + (pTriangle[row-1][col+1]);
//Line 79, exception here.
}
if (col < pTriangle[row].length && row < pTriangle.length) //Move over and give values to indexes recursively until end of row is reached
{ **//Line 87, exception here.**
populateT(row, col+1);
}
else if (col >= pTriangle[row].length && row < pTriangle.length) //If end of row is reached and number of rows is not exceeded.
{
col = 0; //Col reset.
**//Line 93 Exception here.**
populateT(row+1, col);
}
}
}
/* ***********************************************************************************************************************************************
Prints a string containing the values on a given line of the pasTriangle. Note 1 is subtracted from lineNumber to get correct index.
***********************************************************************************************************************************************/
public String getLine(int lineNumber)
{
lineNumber = lineNumber - 1;
String result = "";
for(int biz = 0; biz < pTriangle[lineNumber].length; biz++)
{
result += Integer.toString(pTriangle[lineNumber][biz]);
}
System.out.println(result+"/n");
return result;
}
}
{
public static void main (String [] args)
{
try{
pasTriangle T1 = new pasTriangle(1);
pasTriangle T2 = new pasTriangle(9);
pasTriangle T3 = new pasTriangle(3);
pasTriangle T4 = new pasTriangle(5); //Triangle with only one line created (so not a triangle); test for condition size == 1.
T1.populateT(0, 0);
T2.populateT(0, 0);
T3.populateT(0, 0);
T4.populateT(0, 0);
T1.getLine(1);
T2.getLine(4);
T2.getLine(9); //Test for last line.
T3.getLine(1); //Test for first line.
T3.getLine(2);
T4.getLine(1); //Test for first line.
}
catch(ArrayIndexOutOfBoundsException exception)
{
exception.printStackTrace();
}
}
}
回答1:
The ArrayIndexOutOfoundsException
appears to be coming because this line allows the recursion to continue with an invalid row
index.
else if (col >= pTriangle[row].length && row < pTriangle.length)
The row
variable could be pTriangle.length - 1
, and then you call populateT(row+1, col)
, passing pTriangle.length
into the recursive call. Then that recursive call eventually attempts to access an invalid row
index, causing the exception. Changing it to row < pTriangle.length - 1
will solve the immediate exception problem.
The condition directly above it, row < pTriangle.length
, controls when to move to the next column, but you don't need it here; you're not modifying row
here.
Also, you will want to stop the column recursion when you match row
, not when you match the physical end of the row
, so both conditions need to change. Change if (col < pTriangle[row].length && row < pTriangle.length)
to if (col < row)
, and else if (col >= pTriangle[row].length && row < pTriangle.length)
to else if (col >= row && row < pTriangle.length - 1)
.
Above those conditions, similar changes are needed in the code to determine whether to write a 1
or add the relevant numbers from the row above. Change else if (row != 0 && col == 0 || col == pTriangle[row].length-1)
to else if (row != 0 && col == 0 || col == row)
and else if(row > 1 && col != 0 && col != pTriangle[row].length-1)
to else if(row > 1 && col != 0 && col != row)
.
When you add the previous row's elements to write a non-1
value, it appears you're adding the wrong elements. Assuming that a valid data structure would look like this...
[1][0][0][0]
[1][1][0][0]
[1][2][1][0]
[1][3][3][1]
You need to add the element above and to the left to the element directly above. Change (pTriangle[row-1][col-1]) + (pTriangle[row-1][col+1]);
to (pTriangle[row-1][col-1]) + (pTriangle[row-1][col]);
. (The col+1
changes to col
.)
If you change your output code in getLine
to add a space, you'll be able to verify your numbers better. Also, println
already prints a new line after your parameter, so you don't need to append a newline character (which is \n
instead of /n
).
来源:https://stackoverflow.com/questions/31883108/cannot-get-pascals-triangle-recursive-program-to-work-java