Java.util.AbstractStringBuilder Source Code Analysis

Source: Internet
Author: User

Abstractstringbuilder is an abstract class, is the parent of StringBuilder and StringBuffer, the analysis of its source code to StringBuilder and stringbuffer the understanding of a lot of help.

Let's take a look at the declaration of this class:

Abstract class Implements Appendable, Charsequence {}

This class implements the Appendable and Charsequence interfaces.

Member variables:

Char [] value;//character array for storing strings The int count;//value is the length of the string array being used. Note that the character array is used in length that is not equal to the character array length

Constructor:

Abstractstringbuilder () {//No parameter constructor}abstractstringbuilder (int  capacity) {//with an int argument constructor, Capacity is the size of the string array, which is equivalent        to the size of the string container newchar[capacity];}

Method:

//Returns the actual size of the string
Public
intLength () {returncount;}
return container capacity
Public intcapacity () {returnValue.length;}
Make sure the container size is larger than Minimumcapcipty
Public voidEnsurecapacity (intminimumcapacity) { if(Minimumcapacity > 0) ensurecapacityinternal (minimumcapacity);}Private voidEnsurecapacityinternal (intminimumcapacity) { //overflow-conscious Code if(Minimumcapacity-value.length > 0)//If the minimumcapacity is larger than the container expandcapacity (minimumcapacity);//expansion}voidExpandcapacity (intminimumcapacity) { intnewcapacity = value.length * 2 + 2;//new capacity is (original capacity size + 1)if(Newcapacity-minimumcapacity < 0)//If the expansion is still not enough minimumcapacity newcapacity=minimumcapacity;//So new capacity is minimumcapacityif(Newcapacity < 0) { if(Minimumcapacity < 0)//Overflow Throw NewOutOfMemoryError (); Newcapacity=Integer.max_value; } Value=arrays.copyof (value, newcapacity);//Create New Container}
This method is to change the size of the container to the same size as the string
Public voidtrimtosize () {if(Count <value.length) {value=arrays.copyof (value, count); }}
Set the length of the string, if it is longer than the original, and the extra part fills '% ', if shorter than the original, truncate
Public voidSetLength (intnewlength) { if(Newlength < 0) Throw Newstringindexoutofboundsexception (newlength); Ensurecapacityinternal (newlength); if(Count <newlength) {Arrays.fill (value, Count, Newlength,' + '); } Count=Newlength;}
Returns the character of the index position
Public CharCharAt (intindex) { if(Index < 0) | | (Index >=count)) Throw Newstringindexoutofboundsexception (index); returnValue[index];}
Note the following Append method, which is overloaded with a number of methods, supports append of various types of parameters, only lists parts
Adds a string at the end of the string that is determined by the ToString method of obj
Publicabstractstringbuilder Append (Object obj) {returnAppend (string.valueof (obj));
Adds a string to the end of the string str
Publicabstractstringbuilder Append (String str) {if(str = =NULL) returnAppendnull (); intLen =str.length (); Ensurecapacityinternal (Count+Len); Str.getchars (0, Len, value, count); Count+=Len; return This;}
Add a null string after a string
PrivateAbstractstringbuilder Appendnull () {intc =count; Ensurecapacityinternal (c+ 4); Final Char[] Value = This. Value; Value[c+ +] = ' n '; Value[c+ +] = ' u '; Value[c+ +] = ' l '; Value[c+ +] = ' l '; Count=C; return This;}
Deletes the start-to-end character string, the capacity is constant, and the string length is reduced by End-start
PublicAbstractstringbuilder Delete (intStartintend) { if(Start < 0) Throw Newstringindexoutofboundsexception (start); if(End >count) End=count; if(Start >end)Throw Newstringindexoutofboundsexception (); intLen = end-start; if(Len > 0) {system.arraycopy (value, start+len, value, start, count-end); Count-=Len; }}
Returns a substring of start to end
PublicString substring (intStartintend) { if(Start < 0) Throw Newstringindexoutofboundsexception (start); if(End >count)Throw Newstringindexoutofboundsexception (end); if(Start >end)Throw NewStringindexoutofboundsexception (End-start);
return new String (value, start, End-start);}
Insert STR at index position starting with offset, len-length substring
PublicAbstractstringbuilder Insert (intIndexChar[] str,intOffset,intLen) { if(Index < 0) | | (Index >length ())) Throw Newstringindexoutofboundsexception (index); if((Offset < 0) | | (Len < 0) | | (Offset > Str.length-len)) Throw NewStringindexoutofboundsexception ("Offset" + offset + ", Len" + Len + ", str.length" +str.length); Ensurecapacityinternal (Count+Len); System.arraycopy (value, index, value, index+ Len, Count-index); System.arraycopy (str, offset, value, index, Len); Count+=Len;}
Reverse the order of the strings, such as "ABC" reverse into "CBA"
PublicAbstractstringbuilder Reverse () {BooleanHassurrogates =false; intn = count-1; for(intj = (n-1) >> 1; J >= 0; j--) { intk = N-J; CharCJ =Value[j]; CharCK =Value[k]; VALUE[J]=ck; VALUE[K]=CJ; if(Character.issurrogate (CJ) | |character.issurrogate (CK)) {Hassurrogates=true; } } if(hassurrogates) {reverseallvalidsurrogatepairs (); } return This;}

//... There are other ways.

You can see that abstractstringbuilder almost defines the operation of all strings, and it accepts any type of stitching, operates a string on a dynamic array, and, for frequent character manipulation, uses string to manipulate new string, can improve efficiency.

Java.util.AbstractStringBuilder Source Code Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.