• 学习java第39天


    1.StringBuffer可以追加字符

    public class SyrinBufferDemo1 {
     public static void main(String[] args) {
      StringBuilder buf = new StringBuilder();
      System.out.println(buf.capacity());
      buf.append("张三").append("常说")
       .append("他爱打游戏");
      System.out.println(buf.capadity());
      System.out.println(buf);
     }
    }

    //16

    //34

    //张三常说他爱打游戏

    2.StringBuffer的添加和删除功能

    *Public StringBuffer append ( String str );

    public StringBuffer insert  (  int offset , String str  );

    *public StringBuffer deleteCharAt (  int  index );

    public StringBuffer delete (   int   start  ,int  end );

    public static void main(String[] args) {
    StringBuffer sb =new StringBuffer();
    sb.append("hehehe");
    System.out.println("sb: "+ sb);
    sb.insert(6, "hahahaha");
    System.out.println(sb);
    StringBuffer sb2= new StringBuffer();
    sb2.append("hello").append("World");
    System.out.println(sb2);
    sb2.delete(5, 10)
    System.out.println(sb2);
    sb2.deleteCharAt(4);
    System.out.println(sb2);
    }

    3.stringBuffer性能比String性能高

    public class StringInterview {
     public static void main(String[] args) {
      String result="";
      StringBuffer sbResult=new StringBuffer();
      int count=5000;
      long startTime=System.currentTimeMillis();
      for (int i = 0; i <count; i++)="" {<br="">   result=result+"a";
      }
      System.out.println("string时间差:"+(System.currentTimeMillis()-startTime));
       startTime=System.currentTimeMillis();
      for (int i = 0; i <count; i++)="" {<br="">   sbResult.append("a");
      }
      System.out.println("stringbuffer时间差:"+(System.currentTimeMillis()-startTime));
     }
    }

    //string时间差:32

    //stringbuffer时间差:0

    4.明天学习内容:StringBuilder类的使用

       

  • 相关阅读:
    特性和属性
    装箱和拆箱的问题(NET1.1+)
    poj 1013 Counterfeit Dollar(模拟)
    poj 3686 The Windy's( KM算法 )
    二分匹配(匈牙利算法)
    poj 3273 Monthly Expense(二分)
    poj 2115 C Looooops(扩展欧几里德)
    poj 2400 Supervisor, Supervisee
    poj 2195 Going Home (KM算法)
    poj 2513 Colored Sticks(trie树 + 并查集 + 欧拉图)
  • 原文地址:https://www.cnblogs.com/SirNie/p/13498838.html
Copyright © 2020-2023  润新知