AbstractStringBuilder 源码分析
2015-12-17 19:04
387 查看
类定义
[code]abstract class AbstractStringBuilder implements Appendable, CharSequence
实现了Appendable接口,可序列化
实现了CharSequence接口: 是 char 值的一个可读序列。此接口对许多不同种类的 char 序列提供统一的只读访问。
构造函数
[code] AbstractStringBuilder(int capacity) { value = new char[capacity]; }
创建一个容量为capacity的字符串
方法
[code]public int codePointAt(int index) { if ((index < 0) || (index >= count)) { throw new StringIndexOutOfBoundsException(index); } return Character.codePointAtImpl(value, index, count); }
codePointAt方法中都是用Character.codePointAtImpl(value, index, count)来实现的
[code]public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { if (srcBegin < 0) throw new StringIndexOutOfBoundsException(srcBegin); if ((srcEnd < 0) || (srcEnd > count)) throw new StringIndexOutOfBoundsException(srcEnd); if (srcBegin > srcEnd) throw new StringIndexOutOfBoundsException("srcBegin > srcEnd"); System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); }
getChars方法的实现用的是System.arraycopy()方法
[code]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; }
append方法都牵扯到了ensureCapacityInternal()方法和getChars()方法来实现
[code]void expandCapacity(int minimumCapacity) { int newCapacity = value.length * 2 + 2; if (newCapacity - minimumCapacity < 0) newCapacity = minimumCapacity; if (newCapacity < 0) { if (minimumCapacity < 0) // overflow throw new OutOfMemoryError(); newCapacity = Integer.MAX_VALUE; } value = Arrays.copyOf(value, newCapacity); }
使用了Arrays.copyOf()来实现
[code]public void setLength(int newLength) { if (newLength < 0) throw new StringIndexOutOfBoundsException(newLength); ensureCapacityInternal(newLength); if (count < newLength) { Arrays.fill(value, count, newLength, '\0'); } count = newLength; }
Arrays.fill(value, count, newLength, ‘\0’);字符串之间的复制
[code]public AbstractStringBuilder delete(int start, int end) { if (start < 0) throw new StringIndexOutOfBoundsException(start); if (end > count) end = count; if (start > end) throw new StringIndexOutOfBoundsException(); int len = end - start; if (len > 0) { System.arraycopy(value, start+len, value, start, count-end); count -= len; } return this; }
delete() 仅改变字符串的大小并未真正的删除字符串
[code] public AbstractStringBuilder insert(int index, char[] str, int offset, int len) { if ((index < 0) || (index > length())) throw new StringIndexOutOfBoundsException(index); if ((offset < 0) || (len < 0) || (offset > str.length - len)) throw new StringIndexOutOfBoundsException( "offset " + offset + ", len " + len + ", str.length " + str.length); ensureCapacityInternal(count + len); System.arraycopy(value, index, value, index + len, count - index); System.arraycopy(str, offset, value, index, len); count += len; return this; }
学会灵活的运用System.arraycopy()方法
相关文章推荐
- 一个通过反射改变request的请求参数的方法
- UIAlertController小示例
- UIScrollView详解
- UITextField placeholder 改变默认字体颜色
- <c:out value="" />出错
- SOUI开篇之作
- Microsoft CryptoAPI加密技术(一)[转载vckbase作者Cuick]
- Microsoft CryptoAPI加密技术(二)[转载vckbase作者Cuick]
- 11-UIView与核心动画对比
- cryptAcquireContext函数
- C 语言循环之break、continue
- UVAlive11324 The Largest Clique(scc+dp)
- 开源社区提交patch的工作流程分析
- UITableView
- 使用Hibernate SQLQuery执行原生SQL
- 第十章 设计用户界面 之 构建UI布局
- Android Open Source:陌陌(momo)源代码及实现(大部分UI+功能)
- Leetcode199: Longest Increasing Subsequence
- StoryBoard中使用Segue实现ViewController之间传值
- UIsearchBar背景颜色的改变,占位字符的颜色改变,占位字符字体大小改变