一杯时光 2015-06-27 10:11 采纳率: 0%
浏览 3095

Java性能优化-----for循环优化

要实现一个重组网址的功能,把可能出现的情况给穷举出来,下面代码已经实现(写的比较烂,请见谅),但是在网址比较长的情况下,重组网址的时候遇到大循环问题,导致内存溢出,请各位大侠给优化一下

    public static void main(String [] args) {
        dealUrl("www。qΒ5。c Om");
    }

    private static void dealUrl(String url) {
        char[] ac = url.toLowerCase().toCharArray();
        List<List<String>> charList = new ArrayList<List<String>>();
        List<String> tempList = null;
        for (int i = 0; i < ac.length; i++) {
            tempList = new ArrayList<String>();
            int charIntValue = (int) ac[i];
            if (Character.isDigit(ac[i])) {//数字
                tempList.add(ac[i]+"");
            } else if (charIntValue == 46 ||charIntValue == 65294 ){ //点
                tempList.add(((char) 46)+"");//半角点
                tempList.add(((char) 65294)+"");//全角点
                tempList.add("。");//句号
            } else if (charIntValue == 79 || charIntValue == 65359 
                    || charIntValue == 111 || charIntValue == 65327){//字母o
                tempList.add(((char) 79)+"");//半角o
                tempList.add(((char) 65359)+"");//全角o
                tempList.add(((char) 111)+"");//半角大写o
                tempList.add(((char) 65327)+"");//全角大写o
                tempList.add("0");//零
            } else if(charIntValue == 12288 || charIntValue == 32) {//空格
                tempList.add(((char) 32)+"");//半角空格
                tempList.add(((char) 12288)+"");//全角空格
            } else if (charIntValue >= 65281 && charIntValue <= 65374) {//全角
                char n = (char) (charIntValue - 65248);
                tempList.add(ac[i]+"");//全角字符
                tempList.add(n+"");//半角字符
                tempList.add((ac[i]+"").toUpperCase());//全角大写
                tempList.add((n+"").toUpperCase());//半角大写
            } else {
                char n = (char) (65248+charIntValue);
                tempList.add(ac[i]+"");//半角字符
                tempList.add(n+"");//全角字符
                tempList.add((ac[i]+"").toUpperCase());//半角大写
                tempList.add((n+"").toUpperCase());//全角大写
            }
            charList.add(tempList);
        }
        //分组处理
        List<List<String>> partChongZuList = new ArrayList<List<String>>();
        List<List<String>> partList = null;
        int allSize = charList.size();
        int fenGe = 4;
        int partCount = allSize/fenGe;
        if (partCount > 0 ) {
            for (int i = 0; i < allSize/fenGe; i++) {
                int tempSize = fenGe*(i+1);
                if(allSize/tempSize == 1) {
                    tempSize = allSize;
                }
                partList = new ArrayList<List<String>>();
                for (int k = i*fenGe; k < tempSize; k++) {
                    partList.add(charList.get(k));
                }
                partChongZuList .add(chongZu(partList));
            }
            List<String> aa = chongZu(partChongZuList);
            System.out.println(aa.size());
        } else {
            List<String> aa = chongZu(charList);
            System.out.println(aa.size());
        }
    }

    /**
     * 重组网址
     * @param charList
     */
    private static List<String> chongZu(List<List<String>> charList){
        List<String> newStr = new ArrayList<String>();
        List<String> tempStr = new ArrayList<String>();
        int allLn = charList.size();
        for (int i = 0; i < allLn; i++) {
            List<String> t = charList.get(i);
            int tSize = t.size();
            for (int j = 0; j < tSize; j++) {
                if (i == 0) {
                    tempStr.add( t.get(j));
                } else {
                    String ns = "";
                    int nSize = newStr.size();
                    for (int k = 0 ; k < nSize; k++) {
                        ns = newStr.get(k);
                        ns += t.get(j);
                        tempStr.add(ns);
                    }
                }
            }
            newStr.clear();
            newStr.addAll(tempStr);
            tempStr.clear();
            System.out.println(newStr.size());
        }
        return newStr;
    }
  • 写回答

1条回答 默认 最新

  • 痘痘飞 2015-06-29 04:07
    关注

    笨办法是在循环中间添加清除操作

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!