我正在尝试运行以下程序,但在StringBuilder附加循环结构期间收到OutOfMemory错误。
感谢您的帮助和时间。
List<String> userList = new ArrayList<String>();
List<String> itemList = new ArrayList<String>();
FileInputStream stream = null;
Scanner scanner = null;
int[][] layout = new int[10672][24303];
int indexItemList = 0;
double temp = 0;
try{
stream = new FileInputStream(fileName);
scanner = new Scanner(stream, "UTF-8");
while (scanner.hasNextLine()){
String line = scanner.nextLine();
if (!line.equals("")){
String[] elems = line.split(",");
if (indexItemList == 0) {
temp = Double.valueOf(elems[1]);
layout[0][0] = (int)temp;
itemList.add(elems[0]);
userList.add(elems[2]);
indexItemList++;
}
else {
boolean itemFound = itemList.contains(elems[0]);
boolean userFound = userList.contains(elems[2]);
int indexItem = 1;
int indexUser = 1;
if ((itemFound) && (userFound)) {
indexItem = itemList.indexOf(elems[0]);
indexUser = userList.indexOf(elems[2]);
temp = Double.valueOf(elems[1]);
layout[indexItem][indexUser] = (int)temp;
}
else if ((itemFound) && (!userFound)) {
userList.add(elems[2]);
indexItem = itemList.indexOf(elems[0]);
indexUser = userList.indexOf(elems[2]);
temp = Double.valueOf(elems[1]);
layout[indexItem][indexUser] = (int)temp;
}
else if ((!itemFound) && (userFound)){
itemList.clear();
itemList.add(elems[0]);
indexUser = userList.indexOf(elems[2]);
temp = Double.valueOf(elems[1]);
layout[indexItemList][indexUser] = (int)temp;
indexItemList++;
}
else if (!((itemFound) && (userFound))) {
itemList.clear();
itemList.add(elems[0]);
userList.add(elems[2]);
indexUser = userList.indexOf(elems[2]);
temp = Double.valueOf(elems[1]);
layout[indexItem][indexUser] = (int)temp;
indexItemList++;
}
}
}
}
if (scanner.ioException() != null){
throw scanner.ioException();
}
}
catch (IOException e){
System.out.println(e);
}
finally{
try{
if (stream != null){
stream.close();
}
}
catch (IOException e){
System.out.println(e);
}
if (scanner != null){
scanner.close();
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < layout.length; i++){
for (int j = 0; j < layout[i].length; j++){
sb.append(layout[i][j] + "");
layout[i][j] = 0;
if (j < layout[i].length - 1){
sb.append(",");
}
}
sb.append("\n");
}
您的文件有200,000行,但是您的2D数组具有259'361'616单元,StringBuilder
的大小将与该数字成比例。您无需存储所有这些信息:这是一个非常空心的矩阵。