zoukankan      html  css  js  c++  java
  • StringBuffer 和 StringBuilder

    (1)
    /**
    * This interface represents an ordered set of characters and defines the
    * methods to probe them.描述有序字符序列的接口。定义了探测这个序列的方法。
    */

    public interface CharSequence 

        /**
         * Returns a {@code CharSequence} from the {@code start} index (inclusive)
         * to the {@code end} index (exclusive) of this sequence.
         *
         * @param start    注意:位于start位置的字符, 是包含在子序列中的第一个字符
         *            the start offset of the sub-sequence. It is inclusive, that
         *            is, the index of the first character that is included in the
         *            sub-sequence.
         * @param end        位于end位置的字符,是不包含在子序列中第一个字符。也就是说,只包含index是 end-1的字符
         *            the end offset of the sub-sequence. It is exclusive, that is,
         *            the index of the first character after those that are included
         *            in the sub-sequence
         * @return the requested sub-sequence.
         * @throws IndexOutOfBoundsException
         *             if {@code start < 0}, {@code end < 0}, {@code start > end},
         *             or if {@code start} or {@code end} are greater than the
         *             length of this sequence.
         */
        public CharSequence subSequence(int start, int end);

    (2)

    /**
    * A modifiable {@link CharSequence sequence of characters} for use in creating
    * and modifying Strings. 
    * {@link StringBuffer} and {@link StringBuilder}.

    */

    可变的字符序列,用于生成和修改String。 这个类的目的是作为StringBuffer和StringBuilder的基类。
    abstract class AbstractStringBuilder 

    维护一个char[] ,count = value.length() 既然value.length是自增的,为什么还要count字段。 其实count只是用于 诸如value[count++] = c; 这样的添加删除操作。

      static final int INITIAL_CAPACITY = 16;//初始大小16
    
      private char[] value; 
    
      private int count;
      private boolean shared;
    
        private void enlargeBuffer(int min) {
            int newCount = ((value.length >> 1) + value.length) + 2;
            char[] newData = new char[min > newCount ? min : newCount];
            System.arraycopy(value, 0, newData, 0, count);
            value = newData;
            shared = false;
        }
        final void appendNull() {
            int newCount = count + 4;
            if (newCount > value.length) {
                enlargeBuffer(newCount);
            }
            value[count++] = 'n';
            value[count++] = 'u';
            value[count++] = 'l';
            value[count++] = 'l';
        }

    (3)

    /**
     * A modifiable {@link CharSequence sequence of characters} for use in creating
     * strings, where all accesses are synchronized. This class has mostly been replaced
     * by {@link StringBuilder} because this synchronization is rarely useful. This
     * class is mainly used to interact with legacy APIs that expose it.
    用于生成String的一个可变字符序列,所有存取操作都是同步synchronized的。通常由StringBuilder取代因为synchronization很少有用。
    这个类主要用于 和它有关的遗留下来的API互通。
    * <p>For particularly complex string-building needs, consider {
    @link java.util.Formatter}. * * <p>The majority of the modification methods on this class return {@code * this} so that method calls can be chained together. For example: * {@code new StringBuffer("a").append("b").append("c").toString()}. * * @see CharSequence * @see Appendable * @see StringBuilder * @see String * @see String#format * @since 1.0 */ public final class StringBuffer extends AbstractStringBuilder implements Appendable, Serializable, CharSequence {
    用于生成String的一个可变字符序列。作为StringBuffer的非并发使用的直接替代品。
    和StringBuffer不同,这个类不是synchronized。
    /**
     * A modifiable {@link CharSequence sequence of characters} for use in creating
     * strings. This class is intended as a direct replacement of
     * {@link StringBuffer} for non-concurrent use; unlike {@code StringBuffer} this
     * class is not synchronized.
     *
     * <p>For particularly complex string-building needs, consider {@link java.util.Formatter}.
     *
     * <p>The majority of the modification methods on this class return {@code
     * this} so that method calls can be chained together. For example:
     * {@code new StringBuilder("a").append("b").append("c").toString()}.
     *
     * @see CharSequence
     * @see Appendable
     * @see StringBuffer
     * @see String
     * @see String#format
     * @since 1.5
     */
    public final class StringBuilder extends AbstractStringBuilder implements
            Appendable, CharSequence, Serializable {

    比较了一下源码,StringBuffer和StringBuilder的唯一区别:

    就是Override了它和StringBuilder的共同父类AbstractStringBuilder中的一些操作,使其变成synchronized方法。

    而StringBuilder没有Override,直接使用的父类中的方法。

    以下只是部分例子:

      @Override
        public synchronized char charAt(int index) {
            return super.charAt(index);
        }
    
        @Override
        public synchronized int codePointAt(int index) {
            return super.codePointAt(index);
        }
    
        @Override
        public synchronized int codePointBefore(int index) {
            return super.codePointBefore(index);
        }
    
        @Override
        public synchronized int codePointCount(int beginIndex, int endIndex) {
            return super.codePointCount(beginIndex, endIndex);
        }
  • 相关阅读:
    POJ 1703 Find them, Catch them
    POJ 2236 Wireless Network
    POJ 2010 Moo University
    POJ 2184 Cow Exhibition
    POJ 3280 Cheapest Palindrome
    POJ 3009 Curling 2.0
    POJ 3669 Meteor Shower
    POJ 2718 Smallest Difference
    POJ 3187 Backward Digit Sums
    POJ 3050 Hopscotch
  • 原文地址:https://www.cnblogs.com/maxiaodoubao/p/4435589.html
Copyright © 2011-2022 走看看