最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳細解讀AbstractStringBuilder類源碼

 更新時間:2017年12月01日 10:51:23   作者:freedom_wei  
這篇文章主要介紹了詳細解讀AbstractStringBuilder類源碼,具有一定參考價值,需要的朋友可以了解下。

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

位置:java.lang包中

聲明: abstract class AbstractStringBuilderimplements Appendable, CharSequence

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

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

字段

 /**
     * The value is used for character storage.
     */
    char value[];
    /**
     * The count is the number of characters used.
     */
    int count;

構(gòu)造器

1、無參構(gòu)造器

AbstractStringBuilder() {
  }

2、創(chuàng)建abstractstringbuilder實現(xiàn)類的對象時指定緩沖區(qū)大小為capacity。

 AbstractStringBuilder(int capacity) {
    value = new char[capacity];
  }

當(dāng)子類StringBuilder或StringBuffer實例化時,會在構(gòu)造器中調(diào)用此構(gòu)造器。

擴充容量

void expandCapacity(int minimumCapacity)

此方法有包訪問權(quán)限,類中有多個方法會調(diào)用此方法,在容量不足時擴充容量。

源碼:

 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);
  }

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

ensureCapacity(int minimumCapacity)

public void ensureCapacity(int minimumCapacity)

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

源碼:

 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方法中都是用Character.codePointAtImpl(value, index, count)來實現(xiàn)的

public int codePointAt(int index) {
    if ((index < 0) || (index >= count)) {
      throw new StringIndexOutOfBoundsException(index);
    }
    return Character.codePointAtImpl(value, index, count);
  }

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

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()方法來實現(xiàn)

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()來實現(xiàn)

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');字符串之間的復(fù)制

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;
  }

學(xué)會靈活的運用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))
      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;
  }

總結(jié)

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

相關(guān)文章

  • Spring JPA聯(lián)表查詢之OneToOne源碼詳解

    Spring JPA聯(lián)表查詢之OneToOne源碼詳解

    這篇文章主要為大家介紹了Spring JPA聯(lián)表查詢之OneToOne源碼詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • Spring Boot使用Value注解給靜態(tài)變量賦值的方法

    Spring Boot使用Value注解給靜態(tài)變量賦值的方法

    這篇文章主要介紹了Spring Boot使用Value注解給靜態(tài)變量賦值的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • Java之理解Redis回收算法LRU案例講解

    Java之理解Redis回收算法LRU案例講解

    這篇文章主要介紹了Java之理解Redis回收算法LRU案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • java高并發(fā)的volatile與Java內(nèi)存模型詳解

    java高并發(fā)的volatile與Java內(nèi)存模型詳解

    這篇文章主要介紹了java高并發(fā)的volatile與Java內(nèi)存模型,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-10-10
  • Ubuntu16.04安裝部署solr7的圖文詳細教程

    Ubuntu16.04安裝部署solr7的圖文詳細教程

    這篇文章主要為大家詳細介紹了Ubuntu16.04安裝部署solr7的圖文詳細教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • 一篇文章總結(jié)Java虛擬機內(nèi)存區(qū)域模型

    一篇文章總結(jié)Java虛擬機內(nèi)存區(qū)域模型

    這篇文章主要介紹了一篇文章總結(jié)Java虛擬機內(nèi)存區(qū)域模型,本篇文章主要來總結(jié)一下Java虛擬機內(nèi)存的各個區(qū)域,以及這些區(qū)域的作用、服務(wù)對象以及其中可能產(chǎn)生的問題,作為大家的面試寶典。,需要的朋友可以參考下
    2019-06-06
  • springboot實現(xiàn)返回文件流

    springboot實現(xiàn)返回文件流

    這篇文章主要介紹了springboot實現(xiàn)返回文件流方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java實現(xiàn)簡易飛機大戰(zhàn)

    java實現(xiàn)簡易飛機大戰(zhàn)

    這篇文章主要為大家詳細介紹了java實現(xiàn)簡易飛機大戰(zhàn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • JDBC連接MySQL并實現(xiàn)模糊查詢

    JDBC連接MySQL并實現(xiàn)模糊查詢

    本文詳細講解了JDBC連接MySQL并實現(xiàn)模糊查詢的方式,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-01-01
  • 通過實例講解springboot整合WebSocket

    通過實例講解springboot整合WebSocket

    這篇文章主要介紹了通過實例講解springboot整合WebSocket,WebSocket為游覽器和服務(wù)器提供了雙工異步通信的功能,即游覽器可以向服務(wù)器發(fā)送消息,服務(wù)器也可以向游覽器發(fā)送消息。,需要的朋友可以參考下
    2019-06-06

最新評論

峨边| 庆城县| 广州市| 江油市| 建瓯市| 安仁县| 静海县| 大同县| 洛浦县| 清原| 灵武市| 上犹县| 曲麻莱县| 乐清市| 察隅县| 建瓯市| 文山县| 祁连县| 泗洪县| 沂南县| 浮山县| 金坛市| 郴州市| 太保市| 寿光市| 遵化市| 宜黄县| 宜州市| 军事| 错那县| 玉田县| 吴忠市| 墨江| 临洮县| 镇远县| 太仓市| 阳泉市| 昌吉市| 雷州市| 宁南县| 仲巴县|