Java學習 (十八)、String、StringBuffer和StringBuilder,定義一個自己的StringBuilder的類,bufferbuilder

來源:互聯網
上載者:User

Java學習 (十八)、String、StringBuffer和StringBuilder,定義一個自己的StringBuilder的類,bufferbuilder

String

Java中的字串值屬於String類,雖然有其它方法表示字串(如字元數組),但Java一般使用String類作為字串的標準格式,Java編譯器把字串值作為String對象;

String對象一旦建立就不能改變。如果需要進行大量的字串修改操作,應該使用StringBuilder/StringBuffer類或者字串數組,最終結果可以被轉換成String對象;

 

StringBuffer:安全執行緒的可變字元序列;

一個類似於String的字串緩衝區,通過某些方法調用可以改變該序列的長度和內容;

每個字串緩衝區都有一定的容量。只要字串緩衝區所包含的字元序列的長度沒有超出此容量,就無需分配新的內部緩衝區數組;

如果記憶體緩衝區溢位,則此容量自動增大;

 

StringBuilder:從JDK1.5開始,為StringBuffer類補充了一個單個線程使用的等價類別,即StringBuffer;

與StringBuffer相比,通常應該優先使用StringBuilder類,因為它支援所有相同的操作,但由於它不執行同步,所有速度更快;

常用方法:

public int length()

public int capacity()

public StringBuffer append(String str)

public StringBuffer insert(int offset,String str)

public int indexOf(String str)

public int indexOf(String str,int fromIndex)

public int lastIndexOf(String str)

public StringBuffer reverse()

public String toString()

1 public class StringBuilderDemo{ 2 public static void main(String []args){ 3 //StringBuilder sb="abc";//無此聲明方式 4 //StringBuilder sb=new StringBuilder();//預設16個字元大小的容量 5 //StringBuilder sb=new StringBuilder(100);//初始化容量大小的動態字串 6 //StringBuilder sb=new StringBuilder("abc"); 7 8 StringBuilder sb=new StringBuilder(); 9 sb.append("hello");10 sb.append(1);11 sb.append(1.5);12 sb.append(true);13 System.out.println(sb.length());14 System.out.println(sb.capacity());15 16 sb.insert(5,"world");17 System.out.println(sb.toString());18 sb.replace(5,7,"el");19 System.out.println(sb.toString());20 System.out.println(sb.indexOf("el"));21 System.out.println(sb.reverse());22 }23 }View Code

 

自訂一個自己的StringBuilder的類

案例介紹:自訂一個MyStringBuilder類來實現StringBuilder的功能;

案例設計:實現append方法追加字串的功能;實現length方法統計字元的個數;實現capacity()方法擷取容器大小;實現toString()方法,完成字串的輸出;

 1 import java.util.Arrays; 2 public class MyStringBuilderDemo{ 3     public static void main(String []args){ 4         MyStringBuilder msb=new MyStringBuilder(); 5         msb.append("hello").append("java").append("12345678"); 6         System.out.println("字元個數:"+msb.length()); 7         System.out.println("總容量大小:"+msb.capacity()); 8         System.out.println("輸出字串:"+msb.toString()); 9     } 10 }11 12 class MyStringBuilder{13     private char[] value;//字元數組14     private int count=0;//字元數組中存放字元的個數15     public MyStringBuilder(){16         value=new char[16];17     }18     public MyStringBuilder(int capacity){19         value=new char[capacity];20     }21     public MyStringBuilder(String str){22         value=new char[str.length()+16];23     }24     //得到字元數組中存放的字元個數25     public int length(){26         return count;27     }28     //返回容器的容量大小29     public int capacity(){30         return value.length;31     }32     //實現字串的添加(追加)33     public MyStringBuilder append(String str){34         int len=str.length();//擷取要添加的字串的長度35         //確保字元數組能放進去所添加的字串36         ensureCapacity(count+len);37         //把要添加的字串追加到新的指定數組的指定位置後38         str.getChars(0,len,value,count);39         count+=len;//元素的個數增加了len40         return this;41     }42     43     private void ensureCapacity(int capacity){44         //資料超出容量大小45         if(capacity-value.length>0){46             int newCapacity=value.length*2+2;//擴容後的新字元數組大小47             value=Arrays.copyOf(value,newCapacity);48         }49     }50     //把字元數組的字元轉換成字串格式51     public String toString(){52         return new String(value,0,count);53     }54 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.