FindBugs小记 - String优化使用StringBuilder .

Method concatenates strings using + in a loop
The method seems to be building a String using concatenation in a loop. In each iteration, the String is

converted to a StringBuffer/StringBuilder, appended to, and converted back to a String. This can lead to a

cost quadratic in the number of iterations, as the growing string is recopied in each iteration.
Better performance can be obtained by using a StringBuffer (or StringBuilder in Java 1.5) explicitly.
字符串串联使用方法在一个循环+
**该方法似乎是建立在循环使用字符串串联。在每次迭代中,字符串转换为一个StringBuffer / StringBuilder的,附加到并转换回为String。这可能导致成本的二次迭代,因为不断增长的字符串是在每次迭代中重新复制。
更好的性能,可使用StringBuffer(或StringBuilder的)会更好一些。**

public static String queryOneRecord(final SQLiteDatabase db, final String tableName, final String[] columns,
                                        final String splitStr, final String selection, final String[] selectionArgs,
                                        final String orderBy) {
        Cursor cs = db.query(tableName, columns, selection, selectionArgs, null, null, orderBy);

//        String returnstr = null;
//        int columnsLength = 0;
//        if (columns != null) {
//            columnsLength = columns.length;
//        }
//        if (cs.moveToNext()) {
//            returnstr = "";
//            String columnsVale = null;
//            for (int j = 0; j < columnsLength; j++) {
//                columnsVale = cs.getString(cs.getColumnIndex(columns[j]));
//                if (columnsVale != null) {
//                    returnstr += columnsVale;
//                }
//                if (j != columnsLength - 1) {
//                    returnstr += splitStr;
//                }
//            }
//        }
//        cs.close();
//
//        return returnstr;
        StringBuffer returnstr = new StringBuffer();
        int columnsLength = 0;
        if (columns != null) {
            columnsLength = columns.length;
        }
        if (cs.moveToNext()) {
            String columnsVale = null;
            for (int j = 0; j < columnsLength; j++) {
                columnsVale = cs.getString(cs.getColumnIndex(columns[j]));
                if (columnsVale != null) {
                    returnstr.append(columnsVale);
                }
                if (j != columnsLength - 1) {
                    returnstr.append(splitStr);
                }
            }
        }
        cs.close();

        return returnstr.toString();
    }
        StringBuffer returnstr = new StringBuffer();
//        String returnstr = null;
/** StringBuffer new/delete/setLength 清空数据效率 setLength 最高 */
            returnstr.setLength(0);
//            returnstr = "";

另外,百度查到如果要用到StringBuffer 清空数据,效率 setLength(0)>new StringBuffer(“”)>delete;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值