HelloWorld


  • 首页

  • 分类

  • 归档

  • 标签

  • 关于

  • 其他

EZLippi-浮生志

字符拼接基础回忆录

发表于 2019-11-22 11:34:27 | 分类于 Java | 点击量 5198 ℃

StringBuffer、StringBuilder extends AbstractStringBuilder
StringBuffer.append:
    @Override
    public synchronized StringBuffer append(String str) {
        toStringCache = null;
        super.append(str);
        return this;
    }
注:带有同步锁。
StringBuilder.append:
 @Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }
 
拼接方式 
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;
    } 
//扩容并设置数据 
private void ensureCapacityInternal(int minimumCapacity) {
        // overflow-conscious code
        if (minimumCapacity - value.length > 0) {
            value = Arrays.copyOf(value,
                    newCapacity(minimumCapacity));
        }
    } 
 
//扩充长度
private int newCapacity(int minCapacity) {
        // overflow-conscious code
        int newCapacity = (value.length << 1) + 2;
        if (newCapacity - minCapacity < 0) {
            newCapacity = minCapacity;
        }
        return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)
           ? hugeCapacity(minCapacity)
            : newCapacity;
    }
//拼接数据 
public static char[] copyOf(char[] original, int newLength) {
        char[] copy = new char[newLength];
        System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));
        return copy;
    } 
//遍历长度并重新组合数据 
public static void arraycopy(char[] src, int srcPos, char[] dst, int dstPos, int length) {
        if (src == null) {
            throw new NullPointerException("src == null");
        }
        if (dst == null) {
            throw new NullPointerException("dst == null");
        }
        if (srcPos < 0 || dstPos < 0 || length < 0 ||
            srcPos > src.length - length || dstPos > dst.length - length) {
            throw new ArrayIndexOutOfBoundsException(
                "src.length=" + src.length + " srcPos=" + srcPos +
                " dst.length=" + dst.length + " dstPos=" + dstPos + " length=" + length);
        }
        if (length <= ARRAYCOPY_SHORT_CHAR_ARRAY_THRESHOLD) {
            // Copy char by char for shorter arrays.
            if (src == dst && srcPos < dstPos && dstPos < srcPos + length) {
                // Copy backward (to avoid overwriting elements before
                // they are copied in case of an overlap on the same
                // array.)
                for (int i = length - 1; i >= 0; --i) {
                    dst[dstPos + i] = src[srcPos + i];
                }
            } else {
                // Copy forward.
                for (int i = 0; i < length; ++i) {
                    dst[dstPos + i] = src[srcPos + i];
                }
            }
        } else {
            // Call the native version for longer arrays.
            arraycopyCharUnchecked(src, srcPos, dst, dstPos, length);
        }
    } 
 到此数据基本组合完成
SBuffer同步导致变慢,因此SBuilder>SBuffer。

  • 本文作者: tanbo
  • 本文标题: 字符拼接基础回忆录
  • 本文链接: /blog/articles/81
  • 发布时间: 2019-11-22 11:34:27
< HashMap笔记,主要记录一下常用方法分析
Service分析  >
EZLippi

Tanbo

From Sichuan
Android开发工程师

29 日志
6 分类
6 标签
头像
评论 0
暂无评论
© 2019 - 2020 HelloWorld
由 Hexo 强力驱动
主题 - NexT.Mist | Hosted by Coding Pages