zoukankan      html  css  js  c++  java
  • Java自学-数字与字符串 StringBuffer

    Java StringBuffer常见方法

    StringBuffer是可变长的字符串

    示例 1 : 追加 删除 插入 反转

    append追加
    delete 删除
    insert 插入
    reverse 反转

    package character;
      
    public class TestString {
      
        public static void main(String[] args) {
            String str1 = "let there ";
     
            StringBuffer sb = new StringBuffer(str1); //根据str1创建一个StringBuffer对象
            sb.append("be light"); //在最后追加
             
            System.out.println(sb);
             
            sb.delete(4, 10);//删除4-10之间的字符
             
            System.out.println(sb);
             
            sb.insert(4, "there ");//在4这个位置插入 there
             
            System.out.println(sb);
             
            sb.reverse(); //反转
             
            System.out.println(sb);
     
        }
      
    }
    

    示例 2 : 长度 容量

    为什么StringBuffer可以变长?
    和String内部是一个字符数组一样,StringBuffer也维护了一个字符数组。 但是,这个字符数组,留有冗余长度
    比如说new StringBuffer("the"),其内部的字符数组的长度,是19,而不是3,这样调用插入和追加,在现成的数组的基础上就可以完成了。
    如果追加的长度超过了19,就会分配一个新的数组,长度比原来多一些,把原来的数据复制到新的数组中,看上去 数组长度就变长了
    length: “the”的长度 3
    capacity: 分配的总空间 19

    注: 19这个数量,不同的JDK数量是不一样的

    package character;
      
    public class TestString {
      
        public static void main(String[] args) {
            String str1 = "the";
     
            StringBuffer sb = new StringBuffer(str1);
             
            System.out.println(sb.length()); //内容长度
             
            System.out.println(sb.capacity());//总空间
      
        }
      
    }
    

    练习StringBuffer性能

    生成10位长度的随机字符串
    然后,先使用String的+,连接10000个随机字符串,计算消耗的时间
    然后,再使用StringBuffer连接10000个随机字符串,计算消耗的时间

    提示: 使用System.currentTimeMillis() 获取当前时间(毫秒)

    答案

    package character;
     
    public class TestString {
     
        public static void main(String[] args) {
            int total = 10000;
            String s = randomString(10);
            StringBuffer sb = new StringBuffer();
             
            String str1 = "";
            long start = System.currentTimeMillis();
             
            for (int i = 0; i <total; i++) {
                str1+=s;
            }
            long end = System.currentTimeMillis();
            System.out.printf("使用字符串连接+的方式,连接%d次,耗时%d毫秒%n",total,end-start);
            total *=100;
            start = System.currentTimeMillis();
            for (int i = 0; i <total; i++) {
                sb.append(s);
            }
            end = System.currentTimeMillis();
            System.out.printf("使用StringBuffer的方式,连接%d次,耗时%d毫秒%n",total,end-start);
             
        }
     
        private static String randomString(int length) {
            String pool = "";
            for (short i = '0'; i <= '9'; i++) {
                pool += (char) i;
            }
            for (short i = 'a'; i <= 'z'; i++) {
                pool += (char) i;
            }
            for (short i = 'A'; i <= 'Z'; i++) {
                pool += (char) i;
            }
            char cs[] = new char[length];
            for (int i = 0; i < cs.length; i++) {
                int index = (int) (Math.random() * pool.length());
                cs[i] = pool.charAt(index);
            }
            String result = new String(cs);
            return result;
        }
     
    }
    
  • 相关阅读:
    ubuntu-虚拟机跟主机资源共享的实现方法
    git- 仓库创建、修改、提交、撤销
    theme- 工作原理
    makeMtk- user 版本编译
    Button- 自定义控件添加自定义属性
    actionMode
    screen-Orientation 横竖屏设置
    worktools-mmx 添加编译模块
    eclipse- MAT安装及使用
    worktools-monkey 测试工具的使用
  • 原文地址:https://www.cnblogs.com/jeddzd/p/11633756.html
Copyright © 2011-2022 走看看