6、字符串生成器 String-Builder

package com.xxx.xxx;

public class demo7 {

    /**
     * 字符串生成器    
     * J2SE 可变的字符串序列    String-Builder类
     * @param args
     */
    public static void main(String[] args) {
        String str = "";    //创建空字符串
        //定义对字符串执行操作的起始时间
        long startTime = System.currentTimeMillis();
        for(int i = 0; i < 1000; i++){    //利用 for 循环执行1000次
            str = str + i;    //循环追加字符串
        }
        long endTime = System.currentTimeMillis();    //定义对字符串操作后的时间
        long time = endTime - startTime;        //计算对字符串执行的时间
        System.out.println("Str 消耗时间:"+time);
        //创建字符串
        StringBuilder builder = new StringBuilder("");
        startTime = System.currentTimeMillis();    //定义操作执行前的时间
        for(int j = 0; j < 100000 ; j++ ){
            builder.append(j);        //循环追加字符串
            //System.out.println(builder.append(j));
        }
        endTime = System.currentTimeMillis();
        time = endTime - startTime;
        System.out.println("StringBuilder 消耗时间:"+time);
        
        
    }

}

 

posted @ 2014-12-15 11:42  小菜希  阅读(221)  评论(0编辑  收藏  举报