欢迎使用CSDN-markdown编辑器
来源:互联网 发布:淘宝二手单反镜头骗局 编辑:程序博客网 时间:2024/06/05 18:34
StringBuilder
StringBuilder extends AbstractStringBuilder
/** * Constructs a string builder with no characters in it and an * initial capacity of 16 characters. * 初始化数组容量为16 */ public StringBuilder() { super(16); } /** * 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) { super(capacity); }public AbstractStringBuilder append(String str) { if (str == null) str = "null"; int len = str.length(); ensureCapacityInternal(count + len);/确保容量最小为原来字符数量+新添加的数量 str.getChars(0, len, value, count);//复制新 String 到value数组(value数组经过容量保证判断,放得下新字符串)中,连接到count下标后面。 count += len; return this; } public String toString() { // Create a copy, don't share the array //复制value数组中的字符串,长度为count,此时value数组可能未满即count<Capacity,不共享该value数组,依旧可以继续append return new String(value, 0, count); }
AbstractStringBuilder
char[] vaule;//采用char数组
int count;//数组char数量
//默认容量为16AbstractStringBuilder(int capacity) { value = new char[capacity]; } /** * Returns the length (character count). * * @return the length of the sequence of characters currently * represented by this object * 数组字符长度 */ public int length() { return count; } /** * Returns the current capacity. The capacity is the amount of storage * available for newly inserted characters, beyond which an allocation * will occur. * * @return the current capacity * 数组容量 */ public int capacity() { return value.length; } public void ensureCapacity(int minimumCapacity) { if (minimumCapacity > 0) ensureCapacityInternal(minimumCapacity); } /** * This method has the same contract as ensureCapacity, but is * never synchronized. * 保证数组容量至少大于该minimumCapacity, * 若小于该minimumCapacity,则进行扩容 */ private void ensureCapacityInternal(int minimumCapacity) { // overflow-conscious code if (minimumCapacity - value.length > 0) expandCapacity(minimumCapacity); } /** * This implements the expansion semantics of ensureCapacity with no * size check or synchronization. * 扩容至原来数组字符长度的2倍+2,若依然小于minimumCapacity,则扩容至该值minimumCapacity */ 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); }
0 0
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- @RequestMapping测试各种访问方式
- Regular Expression Matching正则表达式匹配
- Java 负数转换成二进制
- Gradle设置 Java项目classpath
- SQLite3之补充
- 欢迎使用CSDN-markdown编辑器
- linux下安装redis3.2
- 输入正整数n,按从小到大的顺序输出所有形如abcde / fghij = n的表达式,其中a~j恰好为0~9的一个排列,2
- 其它常用类
- 判断一个点是否在指定区域内
- 【记录】eLua初体验
- android xml布局文件中使用RelativeLayout,明明有这个id,却提示找不到---->解决办法
- android xml布局文件中使用RelativeLayout,明明有这个id,却提示找不到---->解决办法
- 某openstack公司面试经历