一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - 詳細解讀AbstractStringBuilder類源碼

詳細解讀AbstractStringBuilder類源碼

2021-02-26 12:52freedom_wei JAVA教程

這篇文章主要介紹了詳細解讀AbstractStringBuilder類源碼,具有一定參考價值,需要的朋友可以了解下。

因為看StringBuffer 和 StringBuilder 的源碼時發現兩者都繼承了AbstractStringBuilder,并且很多方法都是直接super的父類AbstractStringBuilder的方法,所以還是決定先看AbstractStringBuilder的源碼,然后再看StringBuffer 和 StringBuilder.

位置:java.lang包中

聲明: abstract class AbstractStringBuilderimplements Appendable, CharSequence

AbstractStringBuilder 類有abstract 修飾,可知它不能被實例化。

AbstractStringBuilder 類有兩個子類:StringBuilder和StringBuffer。

字段

?
1
2
3
4
5
6
7
8
/**
    * The value is used for character storage.
    */
   char value[];
   /**
    * The count is the number of characters used.
    */
   int count;

構造器

1、無參構造器

?
1
2
AbstractStringBuilder() {
  }

2、創建abstractstringbuilder實現類的對象時指定緩沖區大小為capacity。

?
1
2
3
AbstractStringBuilder(int capacity) {
   value = new char[capacity];
 }

當子類StringBuilder或StringBuffer實例化時,會在構造器中調用此構造器。

擴充容量

void expandCapacity(int minimumCapacity)

此方法有包訪問權限,類中有多個方法會調用此方法,在容量不足時擴充容量。

源碼:

?
1
2
3
4
5
6
7
8
9
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);
 }

將緩沖區長度加1乘2的值賦予變量newCapacity, 然后將此值與指定的值比較,將較大值確定為緩沖區的新容量;然后調用Arrays類的copyof方法,此方法會創建一個新數組,然后將原數組中的字符全部復制進新數組中。

ensureCapacity(int minimumCapacity)

?
1
public void ensureCapacity(int minimumCapacity)

確保容量至少等于指定的最小值。如果當前容量小于指定值,則創建新數組,新數組的容量為指定值的兩倍加2;如果當前容量不小于指定值,則直接不做處理。

源碼:

?
1
2
3
4
5
public void ensureCapacity(int minimumCapacity) {
   if (minimumCapacity > value.length) {
     expandCapacity(minimumCapacity);
   }
 }

測試:

?
1
2
3
4
5
6
7
8
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)來實現的

?
1
2
3
4
5
6
public int codePointAt(int index) {
    if ((index < 0) || (index >= count)) {
      throw new StringIndexOutOfBoundsException(index);
    }
    return Character.codePointAtImpl(value, index, count);
  }

getChars方法的實現用的是System.arraycopy()方法

?
1
2
3
4
5
6
7
8
9
10
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);
  }

append方法都牽扯到了ensureCapacityInternal()方法和getChars()方法來實現

?
1
2
3
4
5
6
7
8
9
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()來實現

?
1
2
3
4
5
6
7
8
9
10
11
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');字符串之間的復制

?
1
2
3
4
5
6
7
8
9
10
11
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() 僅改變字符串的大小并未真正的刪除字符串

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
  }

學會靈活的運用System.arraycopy()方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
 }

總結

以上就是本文關于源碼詳細解讀AbstractStringBuilder類源碼詳細解讀的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

原文鏈接:http://blog.csdn.net/freedom_wei/article/details/50345645

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 美女口述又粗又大感觉 | 亚洲mv国产精品mv日本mv | 欧美xingai| 99久视频 | 91免费播放人人爽人人快乐 | youjizzxxx在线观看 | 视频一区二区国产无限在线观看 | 欧美大陆日韩一区二区三区 | 特黄特色大片免费视频大全 | 亚洲欧美日韩国产一区二区精品 | yy8090韩国日本三理论免费 | 被老外玩爽的中国美女视频 | 胸大的姑娘中文字幕视频 | 高跟丝袜人妖sissy露出调教 | 亚洲精品久久啪啪网站成年 | 98精品全国免费观看视频 | 日韩色在线观看 | 亚州精品视频 | 4p高h三男一女 | 91婷婷射 | 无人区免费一二三四乱码 | 无人区在线观看免费视频国语 | xxx久久| 四虎影院最新网址 | 蜜桃麻豆 | 女人被爽到呻吟娇喘的视频动态图 | japanese乱子mate | 欧美在线一级片 | 色综合伊人色综合网站中国 | 男老头澡堂gay老头456 | 欧美图片另类小说综合 | 精选国产AV精选一区二区三区 | 福利三区| 亚洲娇小性hd | 美国女网址www呦女 美国复古性经典xxxxx | 青青青国产在线 | 久久内在线视频精品mp4 | 日本特黄一级大片 | 99精品国产在现线免费 | 韩国一大片a毛片女同 | 色综合色综合 |