首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java.lang.StringBuilder(2)构造器

2012-12-21 
java.lang.StringBuilder(二)构造器StringBuilder的父类为:java.lang.AbstractStringBuilder这一抽象类,在

java.lang.StringBuilder(二)构造器

StringBuilder的父类为:java.lang.AbstractStringBuilder这一抽象类,在JDK的源码中可以查到,但在JDK6.0的文档却没有此类不知为何,这里就不讨论它。

?? StringBuilder共有4个构造器,如下:

1、StringBuilder() 构造一个其中不带字符的字符串生成器,初始容量为 16 个字符(理解这里返回一个长度为0,初始容量为16的StringBuilder。

??? 源码:

/**     * Constructs a string builder with no characters in it and an      * initial capacity of 16 characters.      */    public StringBuilder() {super(16);//这里直接调用了父类构造器:AbstractStringBuilder(int ..)    }

?AbstractStringBuilder(int)源码:

    /**      * Creates an AbstractStringBuilder of the specified capacity.     */    AbstractStringBuilder(int capacity) {        value = new char[capacity];//得到一个char数组    }

?所以从上面两源码片段,可见StringBuilder()所得到的容量是16的,但由于count没有值,所以长度为0,这一点以后讨论,看第二个构造器

?

2、StringBuilder(CharSequence?seq)? 构造一个字符串生成器,包含与指定的 CharSequence 相同的字符。

/**     * Constructs a string builder that contains the same characters     * as the specified <code>CharSequence</code>. The initial capacity of     * the string builder is <code>16</code> plus the length of the     * <code>CharSequence</code> argument.     *     * @param      seq   the sequence to copy.     * @throws    NullPointerException if <code>seq</code> is <code>null</code>     */    public StringBuilder(CharSequence seq) {//对CharSequence 要理解哟        this(seq.length() + 16);//这里调用了this(int)构造器,设置当前StringBuilder的容量为(seq+16)        append(seq);//调用本类的的append(seq)方法,见下面:
    } /**     * @throws IndexOutOfBoundsException {@inheritDoc} 父类中抛出     */    public StringBuilder append(CharSequence s) {        if (s == null)            s = "null";         if (s instanceof String)            return this.append((String)s);        if (s instanceof StringBuffer)            return this.append((StringBuffer)s);        if (s instanceof StringBuilder)            return this.append((StringBuilder)s);        return this.append(s, 0, s.length());//见以下函数    }    /**     * @throws     IndexOutOfBoundsException {@inheritDoc}     */    public StringBuilder append(CharSequence s, int start, int end) {        super.append(s, start, end);//父类的append见个代码片段        return this;    }父类的   super.append(s, start, end)源码:
 public AbstractStringBuilder append(CharSequence s, int start, int end) {        if (s == null)            s = "null";if ((start < 0) || (end < 0) || (start > end) || (end > s.length()))//长度溢出判断    throw new IndexOutOfBoundsException(//抛出异常                "start " + start + ", end " + end + ", s.length() "                 + s.length());int len = end - start;//len为从中截取的长度if (len == 0)            return this;//如果起始位置相同,则对内容不作处理,返回原对象int newCount = count + len;//count为当前对象的长度 + 截取的长度lenif (newCount > value.length)//如果当前长度大于value的容量则增加对象容量    expandCapacity(newCount);//把当前对象的容量设置为newCount,这里不再讨论此方法        for (int i=start; i<end; i++)//把串中截取部分的值,追加到当前对象的value[]数组,也就是最终的值中            value[count++] = s.charAt(i);//每次加一个        count = newCount;//修改当前对象的长度(理解长度与容量的变化过程)return this;返回当前对象    }
?

?3、StringBuilder(int?capacity)?构造一个其中不带字符的字符串生成器,初始容量由 capacity 参数指定。

???? 只初始化容量,这个构造器在上面提到过,现在来看看它的实现过程:

???? 构造器源码:

??

  /**     * Constructs a string builder with no characters in it and an      * initial capacity specified by the <code>capacity</code> argument.      *     * @param      capacity  the initial capacity.     * @throws     NegativeArraySizeException  if the <code>capacity</code>     *               argument is less than <code>0</code>.     */ public StringBuilder(int capacity) {//只初化一个没有字符内容的(长度为0),容量为capacity的StringBuildersuper(capacity);//调用父类构造器:AbstractStringBuilder(int ..),具体实现见第一构造中有    }

?

?4、StringBuilder(String?str)???构造一个字符串生成器,并初始化为指定的字符串内容。

?

    /**     * Constructs a string builder initialized to the contents of the      * specified string. The initial capacity of the string builder is      * <code>16</code> plus the length of the string argument.       *     * @param   str   the initial contents of the buffer.     * @throws    NullPointerException if <code>str</code> is <code>null</code>     */    public StringBuilder(String str) {super(str.length() + 16);//调用父构造器:AbstractStringBuilder(int ..),注意容量加了16append(str);    }
? public StringBuilder append(String str) {?super.append(str);??????? return this;??? }

父类的append(String)

   /**     * Appends the specified string to this character sequence.     * <p>     * The characters of the <code>String</code> argument are appended, in      * order, increasing the length of this sequence by the length of the      * argument. If <code>str</code> is <code>null</code>, then the four      * characters <code>"null"</code> are appended.     * <p>     * Let <i>n</i> be the length of this character sequence just prior to      * execution of the <code>append</code> method. Then the character at      * index <i>k</i> in the new character sequence is equal to the character      * at index <i>k</i> in the old character sequence, if <i>k</i> is less      * than <i>n</i>; otherwise, it is equal to the character at index      * <i>k-n</i> in the argument <code>str</code>.     *     * @param   str   a string.     * @return  a reference to this object.     */    public AbstractStringBuilder append(String str) {if (str == null) str = "null";        int len = str.length();if (len == 0) return this;int newCount = count + len;if (newCount > value.length)    expandCapacity(newCount);str.getChars(0, len, value, count);//赋值过程,不具体讨论了count = newCount;return this;    }

?好了,写到这里,写得很乱,没有添加什么东西,呵呵,那就写给自己看哈。

?

????????????????????????????????? 2011-01-19? 卓

热点排行