OutOfMemory Error
public static void getAllDataList(List<Data> dataList){
for(int i=0; i<dataList.size(); i++){
String term = dataList.get(i).getTerm() + " ";
String type = dataList.get(i).getType() + " ";
for(int j=i+1; j<dataList.size(); j++){
term = term + dataList.get(j).getTerm() + " ";
type = type + dataList.get(j).getType() + " ";
dataList.add(new Data(type, term, null));
}
}
}
上面这段代码是会出现内存泄露的,因为dataList.size()的值是不停增加的,因为循环里面有dataList.add,所以是个无穷循环。
改正如下:
public static void getAllDataList(List<Data> dataList){
int length = dataList.size();
for(int i=0; i<length; i++){
String term = dataList.get(i).getTerm() + " ";
String type = dataList.get(i).getType() + " ";
for(int j=i+1; j<length; j++){
term = term + dataList.get(j).getTerm() + " ";
type = type + dataList.get(j).getType() + " ";
dataList.add(new Data(type, term, null));
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步