StringBuffer 및 StringBuilder의 소스 코드를 살펴보면 AbstractStringBuilder를 물려 받았으며 많은 방법이 부모 클래스 AbstractStringBuilder의 메소드 인 것으로 밝혀 졌기 때문에 AbstractStringBuilder의 소스 코드를 먼저 살펴보고 StringBuffer 및 StringBuilder를 살펴 보았습니다.
위치 : Java.lang 패키지
진술 : Abstract Class AbstractStringBuilderimplesments 부속 가능한, charseverence
AbstractStringBuilder 클래스에는 초록 수정이 있으며, 이는 인스턴스화 할 수 없음을 보여줍니다.
AbstractStringBuilder 클래스에는 StringBuilder와 StringBuffer의 두 가지 하위 클래스가 있습니다.
전지
/*** 값은 문자 저장에 사용됩니다. */ char value []; /*** 카운트는 사용 된 문자 수입니다. */ int count;
건설자
1. 매개 변수가없는 생성자
AbstractStringBuilder () {}2. AbstractStringBuilder 구현 클래스의 객체를 생성 할 때 버퍼 크기를 용량으로 지정하십시오.
AbstractStringBuilder (int capacity) {value = new char [용량]; }서브 클래스 StringBuilder 또는 StringBuffer가 인스턴스화되면이 생성자는 생성자에서 호출됩니다.
용량 확장
void expandCapacity(int minimumCapacity)
이 방법에는 패키지 액세스 권한이 있으며, 클래스의 여러 메소드는 용량이 불충분 할 때 용량을 확장하기 위해이 방법을 호출합니다.
소스 코드 :
void expandCapicacity (int minimumCapacity) {int newCapacity = (value.length + 1) * 2; if (newCapacity <0) {newCapacity = integer.max_value; } else if (minimumCapacity> newCapacity) {newCapacity = MinimumCapacity; } value = arrays.copyof (value, newCapacity); }버퍼 길이를 변수 newCapacity에 1 x 2 씩 추가 한 다음이 값을 지정된 값과 비교하고 더 큰 값을 버퍼의 새로운 용량으로 결정하십시오. 그런 다음 새로운 배열을 생성 한 배열 클래스의 복사상 메소드를 호출 한 다음 원래 배열의 모든 문자를 새 배열로 복사하십시오.
ensurecapacity (int minimumcapacity)
공개 무가
용량이 지정된 최소값과 적어도 동일해야합니다. 현재 용량이 지정된 값보다 작 으면 새 배열이 생성되고 새 배열의 용량은 지정된 값의 두 배 더하면 2입니다. 현재 용량이 지정된 값보다 작지 않으면 직접 처리되지 않습니다.
소스 코드 :
public void ensurecapacity (int minimumCapacity) {if (minimumCapacity> value.length) {expandCapacity (MinimumCapacity); }}시험:
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 메소드는 arribution.codepointatimpl (value, index, count)을 사용하여 구현됩니다.
public int codepointat (int index) {if ((index <0) || (index> = count)) {throw new stringIndExOutOfBoundSexception (index); } return arribute.codepointAtimpl (값, 색인, 카운트); }GetChars 메소드는 System.ArrayCopy () 메소드를 사용하여 구현됩니다
public void getchars (int srcbegin, int srcend, char [] dst, int dstbegin) {if (srcbegin <0) 새 stringIndexOutOfBoundSexception (srcbegin); if ((srcend <0) || (srcend> count)) 새 StringIndexOutOfBoundSexception (srcend); if (srcbegin> srcend)가 새로운 stringIndexOutOfBoundSexception ( "srcbegin> srcend")을 던지는 경우; System.ArrayCopy (값, SRCBEGIN, DST, DSTBEGIN, SRCENT -SRCBEGIN); }Append 메소드에는 ensurecapacity internal () 메소드와 getChars () 메소드가 포함됩니다.
public acpractStringBuilder Append (String str) {if (str == null) return accendnull (); int len = str.length (); ensurecapacity internal (count + len); str.getchars (0, len, 값, 카운트); count += len; 이것을 반환하십시오; }Array.copyof ()는 구현에 사용됩니다
void expandCapicacity (int minimumCapacity) {int newCapacity = value.length * 2 + 2; if (newCapacity -MinimumCapacity <0) newCapacity = MinimumCapacity; if (newCapacity <0) {if (minimumCapacity <0) // 오버플로가 새로운 outofMemoryError (); newCapacity = integer.max_value; } value = arrays.copyof (value, newCapacity); }Arrays.fill (값, 카운트, Newlength, '/0'); 줄 사이에 복사하십시오
public void setlength (int newlength) {if (newlength <0) 새 stringIndexOutOfBoundSexception (Newlength); ensurecapacity internal (newlength); if (count <newlength) {arrays.fill (value, count, newlength, '/0'); } count = newlength; }delete ()는 문자열의 크기 만 변경하고 실제로 문자열을 삭제하지 않습니다.
public acpractStringBuilder delete (int start, int end) {if (start <0) 새 stringIndexOutOfBoundSexception (시작); if (end> count) end = count; if (start> end) 새 stringIndexOutOfBoundSexception (); int len = end- 시작; if (len> 0) {System.ArrayCopy (값, start+len, value, start, count-end); 카운트 -= 렌; } 이것을 반환하십시오. }System.ArrayCopy () 메소드를 유연하게 사용하는 방법을 배우십시오
Public AbstractStringBuilder Insert (int index, char [] str, int offset, int len) {if ((index <0) || (index> length ())) throw new stringIndExOutOfBoundSexception (index); if ((오프셋 <0) || (len <0) || (Offset> str.length -len)) 새 StringIndexOutOfBoundSexception ( "오프셋" + 오프셋 + ", len" + len + ", str.length" + str.length); ensurecapacity internal (count + len); System.ArrayCopy (값, 색인, 값, 인덱스 + Len, Count -Index); System.ArrayCopy (str, 오프셋, 값, 인덱스, Len); count += len; 이것을 반환하십시오; }요약
위의 내용은 AbstractStringBuilder 클래스 소스 코드의 소스 코드에 대한 자세한 해석에 대한이 기사의 모든 내용입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구는이 사이트의 다른 관련 주제를 계속 참조 할 수 있습니다. 단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!