StringBufferとStringBuilderのソースコードを見ると、両方がAbstractStringBuilderを継承し、多くの方法が直接親クラスの抽象ストリングビルダーの方法であることがわかったため、最初にAbstractStringBuilderのソースコードを調べてから、StringBufferとStringBuilderを調べることにしました。
場所:Java.Langパッケージで
ステートメント:要約クラスAbstractStringStringBuilderimplements Appendable、CharSequence
AbstractStringBuilderクラスには抽象的な変更があり、インスタンス化できないことが示されています。
AbstractStringBuilderクラスには、StringBuilderとStringBufferの2つのサブクラスがあります。
フィールド
/***値は文字ストレージに使用されます。 */ char値[]; /***カウントは使用される文字の数です。 */ int count;
コンストラクタ
1。パラメーターレスコンストラクター
AbstractStringBuilder(){}2. AbstractStringBuilder実装クラスのオブジェクトを作成する場合、バッファサイズを容量に指定します。
abstractStringBuilder(int capacity){value = new char [caperation]; }サブクラスのStringBuilderまたはStringBufferがインスタンス化されると、このコンストラクターはコンストラクターで呼び出されます。
容量を拡大します
void expandCapacity(int minimumCapacity)
この方法にはパッケージアクセス権限があり、クラス内の複数のメソッドがこの方法を呼び出して、容量が不十分な場合に容量を拡大します。
ソースコード:
void endowcapacity(int minimutcapacity){int newcapacity =(value.length + 1) * 2; if(newCapacity <0){newCapacity = integer.max_value; } else if(minimumcapacity> newcapacity){newCapacity = MinimutCapacity; } value = arrays.copyof(value、newcapacity); }バッファーの長さを変数Newcapacityに1 x 2に追加し、この値を指定された値と比較し、バッファーの新しい容量としてより大きな値を決定します。次に、アレイクラスのコピーメソッドを呼び出します。これにより、新しい配列が作成され、元の配列内のすべての文字を新しい配列にコピーします。
EnsureCapacity(int minimutcapacity)
パブリックボイドensurecapacity(int minimutcapacity)
容量が少なくとも指定された最小値に等しいことを確認してください。現在の容量が指定された値よりも少ない場合、新しい配列が作成され、新しい配列の容量は指定された値と2の2倍です。現在の容量が指定された値以上の場合、処理は直接行われません。
ソースコード:
public void ensurecapacity(int minimutcapacity){if(minimumcapacity> value.length){endaxcapacity(minimutcapacity); }}テスト:
stringbuffer s = new StringBuffer(); System.out.println( "容量:" + s.capacity()); //容量:16 S.EnsureCapacity(10); System.out.println( "容量:" + s.capacity()); //容量:16 S.EnsureCapacity(30); System.out.println( "容量:" + s.capacity()); //容量:34 S.EnsureCapacity(80); System.out.println( "容量:" + s.capacity()); //容量:80
方法
CodePointatメソッドは、character.codepointatimpl(value、index、count)を使用して実装されています
public int codePointat(int index){if((index <0)||(index> = count)){new stringindexofboundsexception(index); } return Character.CodePointatimpl(value、index、count); }GetCharsメソッドは、System.ArrayCopy()メソッドを使用して実装されます
public void getchars(int srcbegin、int srcend、char [] dst、int dstbegin){if(srcbegin <0)show new StringIndexOutOfBoundSexception(srcbegin); if((srcend <0)||(srcend> count))をスローします。 if(srcbegin> srcend)show new StringIndexOutOfBoundSexception( "srcbegin> srcend"); System.ArrayCopy(Value、SrcBegin、dst、dstbegin、srcend -srcbegin); }追加方法には、EnsureCapacityInternal()メソッドとそれを実装するgetChars()メソッドが含まれます
public AbstractStringBuilder append(string str){if(str == null)return appendnull(); int len = str.length(); ensurecapacityinternal(count + len); str.getchars(0、len、value、count); count += len;これを返します。 }arrays.copyof()は実装に使用されます
void endaxcapacity(int minimutcapacity){int newcapacity = value.length * 2 + 2; if(newCapacity -MinimutCapacity <0)newCapacity = MinimutCapacity; if(newcapacity <0){if(minimumcapacity <0)//オーバーフローnew outofmemoryerror(); newcapacity = integer.max_value; } value = arrays.copyof(value、newcapacity); }arrays.fill(value、count、newlength、 '/0');文字列間でコピーします
public void setLength(int newlength){if(newLength <0)Throw new StringIndexOutOfBoundSexcection(newLength); ensurecapacityinternal(newlength); if(count <newlength){arrays.fill(value、count、newlength、 '/0'); } count = newLength; }delete()文字列のサイズのみを変更し、文字列を実際に削除しません
public AbstractStringBuilder delete(int start、int end){if(start <0)throw new StringIndexOupBoundSexception(start); if(end> count)end = count; if(start> end)throw new StringIndexOutOfBoundSexception(); int len = end -start; if(len> 0){system.arraycopy(value、start+len、value、start、count-end); count- = len; }これを返します。 }System.ArrayCopy()メソッドを柔軟に使用することを学びます
public AbstractStringBuilder Insert(int index、char [] str、int offset、int len){if((index <0)||(index> length()))throw new StringIndexOutOfBoundSexception(index); if((offset <0)||(len <0)||(offset> str.length -len))新しいstringindexOutofboundsexception( "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;これを返します。 }要約します
上記は、AbstractStringBuilderクラスのソースコードのソースコードの詳細な解釈に関するこの記事のすべての内容です。私はそれが誰にでも役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください。このサイトへのご支援をありがとうございました!