Java 源码 - AbstractStringBuilder
2022/2/17 20:42:19
本文主要是介绍Java 源码 - AbstractStringBuilder,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
介绍
A mutable sequence of characters.
示例
xxxxxx
源码
abstract class AbstractStringBuilder implements Appendable, CharSequence { char[] value; int count; AbstractStringBuilder(int capacity) { value = new char[capacity]; } public int capacity() { return value.length; } 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 AbstractStringBuilder appendNull() { int c = count; ensureCapacityInternal(c + 4); final char[] value = this.value; value[c++] = 'n'; value[c++] = 'u'; value[c++] = 'l'; value[c++] = 'l'; count = c; return this; } public void ensureCapacity(int minimumCapacity) { if (minimumCapacity > 0) ensureCapacityInternal(minimumCapacity); } private void ensureCapacityInternal(int minimumCapacity) { if (minimumCapacity - value.length > 0) { value = Arrays.copyOf(value, newCapacity(minimumCapacity)); } } private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; /** * Returns a capacity at least as large as the given minimum capacity. */ private int newCapacity(int minCapacity) { int newCapacity = (value.length << 1) + 2; if (newCapacity - minCapacity < 0) { newCapacity = minCapacity; } return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0) ? hugeCapacity(minCapacity) : newCapacity; } private int hugeCapacity(int minCapacity) { if (Integer.MAX_VALUE - minCapacity < 0) { // overflow throw new OutOfMemoryError(); } return (minCapacity > MAX_ARRAY_SIZE) ? minCapacity : MAX_ARRAY_SIZE; } 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; } public AbstractStringBuilder replace(int start, int end, String str) { if (start < 0) throw new StringIndexOutOfBoundsException(start); if (start > count) throw new StringIndexOutOfBoundsException("start > length()"); if (start > end) throw new StringIndexOutOfBoundsException("start > end"); if (end > count) end = count; int len = str.length(); int newCount = count + len - (end - start); ensureCapacityInternal(newCount); System.arraycopy(value, end, value, start + len, count - end); str.getChars(value, start); count = newCount; return this; } @Override public abstract String toString(); final char[] getValue() { return value; } }
这篇关于Java 源码 - AbstractStringBuilder的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-08如何用关键链方法突破项目管理瓶颈?
- 2025-01-08电商人必看!6 款提升团队协作与客户满意度软件!
- 2025-01-08电商团队管理混乱?快用这 6 款软件优化协作流程!
- 2025-01-08短剧制作效率低?试试这5款任务管理工具
- 2025-01-08高效应对电商高峰,6 款团队协作软件大揭秘!
- 2025-01-08为什么外贸人都爱上了在线协作工具?
- 2025-01-08提升工作效率,从这些任务管理工具开始
- 2025-01-08新年电商订单暴增,必备的 6 款可视化协作办公软件有哪些?
- 2025-01-08短剧制作经理必备技能与工具全解析
- 2025-01-08在线协作让年货大促轻松应对!