In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shares with you the content of the sample analysis of the AbstractStringBuilder class source code. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Because when looking at the source code of StringBuffer and StringBuilder, it is found that both inherit AbstractStringBuilder, and many methods are the methods of AbstractStringBuilder, the parent of super directly, so I decided to look at the source code of AbstractStringBuilder first, and then look at StringBuffer and StringBuilder.
Location: in the java.lang package
Statement: abstract class AbstractStringBuilderimplements Appendable, CharSequence
The AbstractStringBuilder class is decorated with abstract, so it cannot be instantiated.
The AbstractStringBuilder class has two subclasses: StringBuilder and StringBuffer.
Field
/ * The value is used for character storage. * / char value []; / * The count is the number of characters used. * / int count
Constructor
1. No parameter constructor
AbstractStringBuilder () {}
2. Specify a buffer size of capacity when creating an object of the abstractstringbuilder implementation class.
AbstractStringBuilder (int capacity) {value = new char [capacity];}
This constructor is called in the constructor when the subclass StringBuilder or StringBuffer is instantiated.
Expand capacity
Void expandCapacity (int minimumCapacity)
This method has package access, and several methods in the class call this method to expand capacity when capacity is insufficient.
Source code:
Void expandCapacity (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);}
Assign the buffer length plus 1 by 2 to the variable newCapacity, then compare this value with the specified value to determine the larger value as the new capacity of the buffer; then call the copyof method of the Arrays class, which creates a new array and copies all the characters from the original array into the new array.
EnsureCapacity (int minimumCapacity)
Public void ensureCapacity (int minimumCapacity)
Ensure that the capacity is at least equal to the specified minimum value. If the current capacity is less than the specified value, a new array is created, the capacity of the new array is twice the specified value plus 2; if the current capacity is not less than the specified value, no processing is done directly.
Source code:
Public void ensureCapacity (int minimumCapacity) {if (minimumCapacity > value.length) {expandCapacity (minimumCapacity);}}
Test:
StringBuffer s = new StringBuffer (); System.out.println ("capacity:" + s.capacity ()); / / capacity: 16 s.ensureCapacity (10); System.out.println ("capacity:" + s.capacity ()); / / capacity: 16 s.ensureCapacity (30); System.out.println ("capacity:" + s.capacity ()); / / capacity: 34 s.ensureCapacity (80) System.out.println ("capacity:" + s.capacity ()); / / capacity: 80
Method
CodePointAt methods are all implemented with Character.codePointAtImpl (value, index, count).
Public int codePointAt (int index) {if ((index)
< 0) || (index >= count) {throw new StringIndexOutOfBoundsException (index);} return Character.codePointAtImpl (value, index, count);}
The getChars method is implemented using the System.arraycopy () method
Public void getChars (int srcBegin, int srcEnd, char [] dst, int dstBegin) {if (srcBegin)
< 0) throw new StringIndexOutOfBoundsException(srcBegin); if ((srcEnd < 0) || (srcEnd >Count)) throw new StringIndexOutOfBoundsException (srcEnd); if (srcBegin > srcEnd) throw new StringIndexOutOfBoundsException ("srcBegin > srcEnd"); System.arraycopy (value, srcBegin, dst, dstBegin, srcEnd-srcBegin);}
The append method involves both the ensureCapacityInternal () method and the getChars () method.
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; return this;}
Arrays.copyOf () is used to implement the
Void expandCapacity (int minimumCapacity) {int newCapacity = value.length * 2 + 2; if (newCapacity-minimumCapacity)
< 0) newCapacity = minimumCapacity; if (newCapacity < 0) { if (minimumCapacity < 0) // overflow throw 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 StringIndexOutOfBoundsException(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 StringIndexOutOfBoundsException(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;} return this;}
Learn to use System.arraycopy () flexibly
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) throw new 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; return this;} Thank you for reading! On the "AbstractStringBuilder class source code example analysis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.