posts - 2, comments - 2, trackbacks - 0, articles - 23
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          StringBuilder部分源碼閱讀

          Posted on 2012-02-29 11:01 齊納爾多 閱讀(327) 評論(0)  編輯  收藏 所屬分類: java

          1. StringBuilder extends AbstractStringBuilder implements CharSequence, Serializable

             (1)包含2個屬性->char[] value(底層數(shù)據(jù)結構是數(shù)組), int count(字符串長度) 這2個屬性在AbstractStringBuilder定義
                比String少了一個offset屬性
             (2)構造 public StringBuilder(),調用父類構造, 默認構造一個大小為16的char[]數(shù)組
             (3)append方法,和StringBuffer比較 不是線程安全的

          public StringBuilder append(String str) {
              
          super.append(str);    //調用父類方法
                  return this;        //返回當前對象引用,可以多次執(zhí)行append, 構成一個鏈式調用
            public AbstractStringBuilder append(String str) {
              
          if (str == null) str = "null";        //這里返回的不是空字符""
                  int len = str.length();
              
          if (len == 0return this;
              
          int newCount = count + len;
              
          if (newCount > value.length)        //如果原來字符串的長度+要拼接的字符長度>分配的數(shù)組的長度, 擴容
                  expandCapacity(newCount);
              str.getChars(
          0, len, value, count);    //把字符串添加到數(shù)組里面(如果擴容的話,是添加到新的數(shù)組里面)
              count = newCount;            
              
          return this;
              }
          void expandCapacity(int minimumCapacity) {
              
          int newCapacity = (value.length + 1* 2;    //擴大為(原來字符串的長度+要拼接的字符長度+1)的2倍
                  if (newCapacity < 0{
                      newCapacity 
          = Integer.MAX_VALUE;
                  }
           else if (minimumCapacity > newCapacity) {
                  newCapacity 
          = minimumCapacity;
              }

                  value 
          = Arrays.copyOf(value, newCapacity);  //數(shù)組的copy, 重新構造一個新的數(shù)組,長度為newCapacity
              }

              
               
          //String方法
               public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
                  
          if (srcBegin < 0{
                      
          throw new StringIndexOutOfBoundsException(srcBegin);
                  }

                  
          if (srcEnd > count) {
                      
          throw new StringIndexOutOfBoundsException(srcEnd);
                  }

                  
          if (srcBegin > srcEnd) {
                      
          throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
                  }

                  System.arraycopy(value, offset 
          + srcBegin, dst, dstBegin,
                       srcEnd 
          - srcBegin);
              }

              
              
          //重載
              public AbstractStringBuilder append(CharSequence s, int start, int end) {
                  
          if (s == null)
                      s 
          = "null";
              
          if ((start < 0|| (end < 0|| (start > end) || (end > s.length()))    //Assert
                  throw new IndexOutOfBoundsException(
                          
          "start " + start + ", end " + end + ", s.length() " 
                          
          + s.length());
              
          int len = end - start;
              
          if (len == 0)
                      
          return this;
              
          int newCount = count + len;
              
          if (newCount > value.length)
                  expandCapacity(newCount);    
          //擴容,分配一個新的數(shù)組,
                  for (int i=start; i<end; i++)
                      value[count
          ++= s.charAt(i);
                  count 
          = newCount;
              
          return this;               //返回當前對象的引用,形成鏈式調用
              }

                }

          (4)String源碼中的重載方法

           //從fromIndex位置處開始搜索
              public int indexOf(int ch, int fromIndex) {
              
          int max = offset + count;  //字符串長度
              char v[] = value;       //備份一個數(shù)組
              
              
          //校驗判斷
              if (fromIndex < 0{
                  fromIndex 
          = 0
              }
           else if (fromIndex >= count) {
                  
          // Note: fromIndex might be near -1>>>1.
                  return -1;
              }


              
          int i = offset + fromIndex;
              
          if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) {
                   
          //BMP--基本多文種平面(Basic Multilingual Plane)
                  
          // handle most cases here (ch is a BMP code point or a
                  
          // negative value (invalid code point))
                  for (; i < max ; i++{
                  
          if (v[i] == ch) {
                      
          return i - offset;
                  }

                  }

                  
          return -1;
              }


              
          if (ch <= Character.MAX_CODE_POINT) {
                  
          //SMP-輔助平面(Supplementary Planes)
                  
          // handle supplementary characters here
                  
          //這里char[]長度為2因為char是2個字節(jié), int(ch)> Character.MIN_SUPPLEMENTARY_CODE_POINT
                  char[] surrogates = Character.toChars(ch);
                  
          for (; i < max; i++{
                  
          if (v[i] == surrogates[0]) {
                      
          if (i + 1 == max) {
                      
          break;
                      }

                      
          if (v[i+1== surrogates[1]) {//如果第一個字符和第2個字符都相當?shù)脑?返回索引
                      return i - offset;
                      }

                  }

                  }

              }

              
          return -1;
              }


           

          主站蜘蛛池模板: 海宁市| 当雄县| 班戈县| 乐清市| 资源县| 行唐县| 沾化县| 静乐县| 海阳市| 治县。| 岫岩| 商都县| 金山区| 张北县| 东光县| 扎鲁特旗| 南城县| 西乌珠穆沁旗| 灌云县| 永泰县| 黔东| 桂平市| 鹿邑县| 清河县| 镇巴县| 台州市| 西盟| 中牟县| 秦安县| 莱阳市| 石泉县| 扎赉特旗| 纳雍县| 兰坪| 睢宁县| 凤阳县| 凌云县| 天水市| 疏附县| 潞城市| 扎鲁特旗|