문제는 다음과 같습니다.
줄에서 int [] previousLineArray = new int [(currentLineNumber+1)];
- 당신은 당신의 멤버를 어둡게하는 로컬 어레이를 만듭니다. if
성명. 그런 다음이 라인에 오면 : nextLineArray[(nextLineCount)] = (previousLineArray[(nextLineCount-1)
초기가 아닌 멤버 배열을 사용합니다.
문제
좋아, 파스칼 삼각형을 재현하는 수업이 필요합니다. 우리는 BlueJ를 사용하고 있으며 배열이 서로 액세스 할 수 없습니다. 코드는 다음과 같습니다.
public class PascalTriangle {
private int currentLineNumber;
private int[] previousLineArray;
private int[] nextLineArray;
public void firstLine()
{
currentLineNumber = 1;
System.out.println("1");
}
public void nextLine()
{
if (currentLineNumber == 1) {
int [] previousLineArray = new int [(currentLineNumber+1)];
previousLineArray[0] = 1;
previousLineArray[1] = 1;
System.out.println("1 1");
currentLineNumber = 2;
}
else if(currentLineNumber >= 2) {
for (int lineCount = currentLineNumber; lineCount <= currentLineNumber; lineCount++) {
int [] nextLineArray = new int [(lineCount+1)];
nextLineArray[0] = 1;
System.out.print(nextLineArray[0] + " ");
for (int nextLineCount = 1; nextLineCount < lineCount; nextLineCount++) {
// The next line is the line with the NullPointerException
nextLineArray[(nextLineCount)] = (previousLineArray[(nextLineCount-1)
+ previousLineArray[(nextLineCount)]]);
System.out.print(nextLineArray[(nextLineCount)] + " ");
}
nextLineArray[(lineCount)] = 1;
System.out.print(nextLineArray[(lineCount)] + "\n");
previousLineArray = nextLineArray;
}
currentLineNumber = currentLineNumber+1;
}
}
}
수업은 컴파일되지만 세 번째 줄에 도달하면 읽어야합니다. 1 2 1
, 나는 얻었다 java.lang.NullPointerException
~에 PascalTriangle.nextLine(PascalTriangle.java:29)
그것은 고도로 nextLineArray[(nextLineCount)] = (previousLineArray[(nextLineCount-1)
선. NextLinearRay가 왜 이전 라인 레이어에서 정보를 가져 오는가? nextLine()
방법이 처음으로 요구됩니까?
모든 도움이 감사하겠습니다 :) 감사합니다.
해결책
문제는 다음과 같습니다.
줄에서 int [] previousLineArray = new int [(currentLineNumber+1)];
- 당신은 당신의 멤버를 어둡게하는 로컬 어레이를 만듭니다. if
성명. 그런 다음이 라인에 오면 : nextLineArray[(nextLineCount)] = (previousLineArray[(nextLineCount-1)
초기가 아닌 멤버 배열을 사용합니다.