創建數組嘿,我有這樣的代碼在這裏:的Java:問題從文件
public class Levels {
boolean newGame = true;
public void newGame() {
while (newGame) {
int cLevel = 1;
List<String> list = new ArrayList<String>();
try {
BufferedReader bf = new BufferedReader(new FileReader(
"src/WordGuess/ReadFile/LevelFiles/Level_" + cLevel
+ ".txt"));
String cLine = bf.readLine();
while (cLine != null) {
list.add(cLine);
}
String[] words = new String[list.size()];
words = list.toArray(words);
for (int i = 0; i < words.length; i++) {
System.out.println(words[i]);
}
} catch (Exception e) {
System.out
.println("Oh! Something went terribly wrong. A team of highly trained and koala-fied koalas have been dispatched to fix the problem. If you dont hear from them please restart this program.");
e.printStackTrace();
}
}
}}
,這讓我這個錯誤:
能Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Unknown Source) at java.util.Arrays.copyOf(Unknown Source) at java.util.ArrayList.grow(Unknown Source) at java.util.ArrayList.ensureExplicitCapacity(Unknown Source) at java.util.ArrayList.ensureCapacityInternal(Unknown Source) at java.util.ArrayList.add(Unknown Source) at WordGuess.ReadFile.SaveLoadLevels.Levels.newGame(Levels.java:24) at Main.main(Main.java:29)
任何人的幫助,好嗎?謝謝!
什麼大小的文件,你正在閱讀?當您的計算機內存不足時會拋出java.lang.OutOfMemoryError,因爲您正在Java中運行非常繁重的程序。 –
@migueljimenezz:在這種情況下,文件的長度是不相關的,只要至少有一行 - 請參閱我的回答:) –
_newGame_設置爲false的位置?我在這裏看到的是一個無限循環! –