Java string buffer (StringBuilder) (xxv)

Why should appear character buffer

As we all know, String class is immutable, but sometimes we have to use string concatenation, a small amount of time if the stitching, can be, but if the amount of data is too large, then splicing, memory consumption on too, so this time then the string class, then it is very inconvenient, you must have a class in string concatenation, not memory-intensive class, and can be spliced. So StringBuffer StringBuilder class and the class came.

There are several character buffer

There are two character buffer:

StringBuilder thread-safe, but efficient.
StringBuffer thread-safe, but the efficiency is not high.

Both classes inherit the AbstractStringBuilderabstract class, so most of them are the same. They are two of the underlying array, the only way it can be carried out without taking up too much memory is available, string concatenation.

Character buffer

The following main talking StringBuilder class:

Construction method

There are four configurations StringBuilder class method, also commonly used in the three, namely:
first: (constructor with no arguments)

    public StringBuilder() {
        super(16);
    }

No argument constructor is configured to directly call the parent class, creating an array of length 16. ·
The parent class's method:

//主要的操作的数组
char[] value;
//父类里面的构造方法
AbstractStringBuilder(int capacity) { 
    value = new char[capacity];
}

The second: (initialized buffer size)

    public StringBuilder(int capacity) {
        super(capacity);
    }

The second method is an array to direct the user to initialize a value transfer.


Third: (string initialized by the buffer)

    public StringBuilder(String str) {
        super(str.length() + 16);
        append(str);
    }

A third method is to pass over a string, plus the length of the string 16, to create a section break out buffer, and then pass over the string to the newly created character buffer.


The method of operation of the common

Add character welfare

    //源码(不光这一个,但是最常用的就是这一个了)
    public StringBuilder append(String str) {
        super.append(str);  //调用父类里面的追加方法,把字符串追加到原数组里面
        return this;           //返回当前对象,说明可以使用链式方法
    }

use:

    StringBuilder sbu = new StringBuilder();
    sbu.append("a");
    sbu.append("b");                //可以一个一个的追加
    System.out.println(sbu);        //ab
    sbu.append("c").append("d");    //也可以在一行里面追加
    System.out.println(sbu);        //abcd

There is someone asked if the length of the array of characters inside the StringBuilder run out how to do, in fact, there is also a source Given this situation, when every appen, will determine what the length of the array is not enough, if not it will automatic expansion, every time expansion is on a capacity of 2 plus 2 times, and then re-create a new array, the original array is copied.

To delete a character

There are two ways to delete a character, you can delete the character at the specified index, or you can delete a bunch of strings. Methods:
public deleteCharAt the StringBuilder (int index) , public the StringBuilder the Delete (Start int, int End)
Use:

    StringBuilder sbu = new StringBuilder("abcdefghijklmnopqrstuvwxyz");
    //删除下标从1到开始到3的字符串
    StringBuilder deleteSbu = sbu.delete(1, 3);
    System.out.println(deleteSbu);      //adefghijklmnopqrstuvwxyz
    //也可指定删除那一个位置的字符    删除位置0的字符
    StringBuilder deleteChar = sbu.deleteCharAt(0);
    System.out.println(deleteChar );    //defghijklmnopqrstuvwxyz

Insert Character

Insert strings are inserted from the insertion place to start before the next standard, such as a character index is 1, then the place if the insert is 1, then the subscript after the character is not the 1.
example:

    StringBuilder sbu = new StringBuilder("abc");
    //从下标为1的地方开始插入字符串
    StringBuilder newSbu = sbu.insert(1, "***");
    System.out.println(newSbu);     //a***bc

To reverse a string

And the String class as a string reverse. Method: public the StringBuilder Reverse ()
Example:

    StringBuilder sbu = new StringBuilder("abc");
    //字符串反转
    System.out.println(sbu.reverse());  //cba

Converted into a String

StringBuilder class when the string concatenation can be used, but most of the time, we still use the String class, it must be converted before they can be satisfying. Method: public String toString ()
Source:

    public String toString() {
        // 就是从新创建一个字符串,再返回来,value是数组,count是数组的长度
        return new String(value, 0, count);
    }

They use:

    StringBuilder sbu = new StringBuilder("abc");
    String s = sbu.toString();      //转换成String类型了

These are the StringBuilder inside the more common method, but there are many ways to refer to API, say no more here, then that fact and the String class which is very similar to StringBuilder there are many methods of operation. There is a StringBuffer and StringBuilder methods inside the class inside the method is actually on usage are the same, is not the same level of security threads.



Details determine success or failure!
Personal humble opinion, if not, ask righting!

Guess you like

Origin www.cnblogs.com/xdtg/p/12593679.html