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));
}
}
}
复制代码



posted on   亭子  阅读(231)  评论(0编辑  收藏  举报

< 2012年3月 >
26 27 28 29 1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7

导航

统计

点击右上角即可分享
微信分享提示