【发布时间】:2014-02-19 22:34:48
【问题描述】:
嘿,我这里有这段代码:
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();
}
}
}}
它给了我这个错误:
线程“主”java.lang.OutOfMemoryError 中的异常:Java 堆空间 在 java.util.Arrays.copyOf(未知来源) 在 java.util.Arrays.copyOf(未知来源) 在 java.util.ArrayList.grow(未知来源) 在 java.util.ArrayList.ensureExplicitCapacity(未知来源) 在 java.util.ArrayList.ensureCapacityInternal(未知来源) 在 java.util.ArrayList.add(未知来源) 在 WordGuess.ReadFile.SaveLoadLevels.Levels.newGame(Levels.java:24) 在 Main.main(Main.java:29)
有人可以帮忙吗?谢谢!
【问题讨论】:
-
您正在阅读的文件有多大?当您的计算机内存不足时抛出 java.lang.OutOfMemoryError,因为您正在使用 Java 运行非常繁重的程序。
-
@migueljimenezz:在这种情况下,文件的长度无关紧要,只要至少有一行 - 请参阅我的回答 :)
-
newGame 在哪里设置为 false?我在这里看到的是一个无限循环!
标签: java arrays arraylist bufferedreader filereader