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

          StringBuilder部分源碼閱讀

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

          1. StringBuilder extends AbstractStringBuilder implements CharSequence, Serializable

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

          public StringBuilder append(String str) {
              
          super.append(str);    //調用父類方法
                  return this;        //返回當前對象引用,可以多次執行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)        //如果原來字符串的長度+要拼接的字符長度>分配的數組的長度, 擴容
                  expandCapacity(newCount);
              str.getChars(
          0, len, value, count);    //把字符串添加到數組里面(如果擴容的話,是添加到新的數組里面)
              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);  //數組的copy, 重新構造一個新的數組,長度為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);    
          //擴容,分配一個新的數組,
                  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;       //備份一個數組
              
              
          //校驗判斷
              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個字節, 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個字符都相當的話,返回索引
                      return i - offset;
                      }

                  }

                  }

              }

              
          return -1;
              }


           

          主站蜘蛛池模板: 红桥区| 汉中市| 元氏县| 朝阳区| 凉城县| 磐安县| 栾城县| 垦利县| 武邑县| 延津县| 桑日县| 滦南县| 武鸣县| 车致| 虎林市| 仁寿县| 清镇市| 阳高县| 丰镇市| 文水县| 普兰店市| 宜都市| 德昌县| 宁远县| 海南省| 北辰区| 石棉县| 高唐县| 平湖市| 怀柔区| 渭南市| 都江堰市| 芜湖县| 科技| 昂仁县| 昆山市| 海南省| 库车县| 广昌县| 闻喜县| 临汾市|