java之AbstractStringBuilder类详解
AbstractStringBuilder类 |
位置:java.lang包中 |
声明: |
AbstractStringBuilder 类有abstract 修饰,可知它不能被实例化。 AbstractStringBuilder 类有两个子类:StringBuilder和StringBuffer。 |
字段 |
/** |
构造器 |
1、无参构造器 AbstractStringBuilder() { |
2、创建abstractstringbuilder实现类的对象时指定缓冲区大小为capacity。 AbstractStringBuilder(int capacity) { 当子类StringBuilder或StringBuffer实例化时,会在构造器中调用此构造器。 |
扩充容量 |
void expandCapacity(int minimumCapacity) |
此方法有包访问权限,类中有多个方法会调用此方法,在容量不足时扩充容量。 |
源码: void expandCapacity(int minimumCapacity) { 将缓冲区长度加1乘2的值赋予变量newCapacity, 然后将此值与指定的值比较,将较大值确定为缓冲区的新容量;然后调用Arrays类的copyof方法,此方法会创建一个新数组,然后将原数组中的字符全部复制进新数组中。 |
ensureCapacity(int minimumCapacity) |
public void ensureCapacity(int minimumCapacity) |
确保容量至少等于指定的最小值。如果当前容量小于指定值,则创建新数组,新数组的容量为指定值的两倍加2;如果当前容量不小于指定值,则直接不做处理。 |
源码: public void ensureCapacity(int minimumCapacity) { |
测试: StringBuffer s = new StringBuffer(); |
length() |
public int length() |
返回缓冲区中的代码单元数。 |
注意:此方法返回的并不是字符的数量,因为对于Unicode增补字符1个代码点对应2个代码单元。可以通过codePointCount方法获取字符数。 |
源码: public int length() { |
Unicode标量值测试: StringBuffer s = new StringBuffer(); Unicode增补字符测试: StringBuffer s = new StringBuffer(); |
capacity() |
public int capacity() |
返回数组value的容量。 |
源码: public int capacity() { |
测试: System.out.println(s.capacity());// |
反转 |
public AbstractStringBuilder reverse() |
将字符序列反转。 |
问题:我们知道对于Unicode 标量值只需要1个char变量即可表示,但对于增补字符却需要2个char变量,反转时如何正确的处理这些字符? 查看reverse源码: public AbstractStringBuilder reverse() { 可知此方法主要有两步: 1、用一个循环反转序列;并在每次循环时判断调换的两个字符是否有一个字符在\uD800和\uDFFF之间,如果有则置hasSurrogate的值为true。 2、若hasSurrogate的值为true,则进入第二个循环,调用Character类的isLowSurrogate和isHighSurrogate方法判断,将反转的增补字符再次反转,以此可确保字符的正确性。 |
添加 |
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
删除 |
public AbstractStringBuilder |
删除字符序列中从start开始,end结束的子序列。 |
插入 |
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
|||||
|
获取索引 |
|||||
|
|||||
|
|||||
|
|||||
|
替换 |
public AbstractStringBuilder replace(int start, int end, String str) |
用字符串str替换原字符序列中从start开始,end结束的子序列。 |
部分代码: if (end > count) |
获取子序列 |
||||
|
||||
|
||||
|
调整空间 |
public void trimToSize() |
试图减少字符序列的存储。如果缓冲区是大于必要的保持其当前的字符序列,然后可调整到更加节省空间。 |
源码: public void trimToSize() { |
测试: StringBuffer sb = new StringBuffer("12345"); |
设置长度 |
public void setLength(int newLength) |
指定字符序列新长度为newLength。 |
源码: public void setLength(int newLength) { |
测试: StringBuffer s = new StringBuffer("123456"); |
获取字符 |
public char charAt(int index) |
返回指定索引处的代码单元。 |
注意:此方法返回的并不一定是字符,因为对于Unicode增补字符1个代码点对应2个代码单元。 |
源码: public char charAt(int index) { |
修改字符 |
public void setCharAt(int index, char ch) |
修改指定索引处(index)的代码单元为指定的代码单元(ch)。 |
源码: public void setCharAt(int index, char ch) { |
删除字符 |
public AbstractStringBuilder deleteCharAt(int index) |
删除字符序列中指定索引处的代码单元。 |
源码: public AbstractStringBuilder deleteCharAt(int index) { |
getChars |
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) |
源数组value从srcBegin开始,srcEnd结束的字符序列依次覆盖目的数组dst中从dstBegin开始的连续(srcEnd - srcBegin)个代码单元。 |
注意:覆盖的字符序列不能越界。 |
源码: public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { |
测试1: StringBuffer s = new StringBuffer("123"); 测试2: StringBuffer s = new StringBuffer("123"); 打印: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4 |
codePointAt(int index) |
public int codePointAt(int index) |
返回指定索引处的字符(Unicode code point)。 |
源码: public int codePointAt(int index) { |
标量值测试: StringBuffer s = new StringBuffer(); 打印: 49 增补字符测试: StringBuffer s = new StringBuffer(); 打印: 65536 从测试中可以看出当字符序列中都是标量值字符时,直接输出对应的码点;当有增补字符时,那么,当索引指向的是高代理项时,就会输出整个增补字符的码点,当索引指向的是低代理项时,就会返回对应代码单元的码点。 |
codePointBefore(int index) |
public int codePointBefore(int index) |
返回指定索引前的字符(Unicode code point)。 |
部分源码: return Character.codePointBefore(value, index); |
标量值测试: StringBuffer s = new StringBuffer(); 打印: 49 增补字符测试: StringBuffer s = new StringBuffer(); 打印: 55296 从测试中可以看出当字符序列中都是标量值字符时,输出指定索引处的码点;当有增补字符时,那么,当索引指向的是高代理项时,就会输出对应代码单元的码点;当索引指向的是低代理项时,就会返回整个增补字符的码点。 |
codePointCount(int beginIndex, int endIndex) |
public int codePointCount(int beginIndex, int endIndex) |
返回在指定的文本范围的Unicode代码点的数量。 |
源码: public int codePointCount(int beginIndex, int endIndex) { 测试1: StringBuffer s = new StringBuffer("123123"); 测试2: StringBuffer s = new StringBuffer(); 从测试中可知对于Unicode标量值1个代码点对应1个char,对于Unicode增补字符1个代码点对应2个char。 |
offsetByCodePoints(int index, int codePointOffset) |
public int offsetByCodePoints(int index, int codePointOffset) |
返回从给定的index处偏移codePointOffset个代码点的索引。 |
源码: public int offsetByCodePoints(int index, int codePointOffset) { |
测试: StringBuffer s = new StringBuffer(); 打印: 1 |
appendCodePoint(int codePoint) |
public AbstractStringBuilder appendCodePoint(int codePoint) |
添加指定代码点对应的字符。 |
源码: public AbstractStringBuilder appendCodePoint(int codePoint) { |
标量值测试: StringBuffer s = new StringBuffer(); 打印: 1 增补字符测试: StringBuffer s = new StringBuffer(); 打印: |
相关链接:
java之AbstractStringBuilder类详解的更多相关文章
- java之StringBuffer类详解
StringBuffer 线程安全的可变字符序列. StringBuffer源码分析(JDK1.6): public final class StringBuffer extends Abstract ...
- java之StringBuilder类详解
StringBuilder 非线程安全的可变字符序列 .该类被设计用作StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍).如果可能,建议优先采用该类,因为在 ...
- java.lang.Thread类详解
java.lang.Thread类详解 一.前言 位于java.lang包下的Thread类是非常重要的线程类,它实现了Runnable接口,今天我们来学习一下Thread类,在学习Thread类之前 ...
- Java中dimension类详解
Java中dimension类详解 https://blog.csdn.net/hrw1234567890/article/details/81217788
- java之Matcher类详解
在JDK 1.4中,Java增加了对正则表达式的支持. java与正则相关的工具主要在java.util.regex包中:此包中主要有两个类:Pattern.Matcher. Matcher 声明: ...
- java的ReentrantLock类详解
ReentrantLock 能用于更精细化的加锁的Java类, 通过它能更清楚了解Java的锁机制 ReentrantLock 类的集成关系有点复杂, 既有内部类, 还有多重继承关系 类的定义 pub ...
- Java的String类详解
Java的String类 String类是除了Java的基本类型之外用的最多的类, 甚至用的比基本类型还多. 同样jdk中对Java类也有很多的优化 类的定义 public final class S ...
- Java Properties工具类详解
1.Java Properties工具类位于java.util.Properties,该工具类的使用极其简单方便.首先该类是继承自 Hashtable<Object,Object> 这就奠 ...
- Java中ArrayList类详解
1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...
随机推荐
- cordova混合开发:Android中native调用javascript
今天学习怎么在java中调用javascript方法,做个记录: 第一种方式,这个最简单: loadUrl("javascript:func1()"); 要注意要在devicere ...
- checkbox与说明文字无法对齐的问题
解决方法: vertical-align:middle; 例:<input type=checkbox id="theId" name=checkbox style=&quo ...
- Intellij IDEA IDE部署Servlet项目
1.设置Project Structure 2.修改Modules中的Web项目文件默认class编译之后输出位置 3.给Modules中的Web项目添加Web模块 4.修改Web项目Web.xml文 ...
- Linux和UNIX监控
Linux和UNIX上的数据库监控工具包括监控CPU.内存.磁盘.网络.安全性和用户的监控工具.下面罗列了我们找到的有用工具及其简单描述. ps 显示系统上运行的进程列表 top ...
- nginx 安装与反向代理测试 under MAC
安装 在 Mac 下可以直接使用 homebrew 安装 nginx brew search nginx brew install nginx 启动 nginx: sudo nginx,访问 8080 ...
- PyCharm4注册码--软件安装
软件连接:http://www.jetbrains.com/pycharm/ PyCharm4注册码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...
- [Z] 关于c++ typename的另一种用法
在看c++ primer的时候见到了一下这种用法: typedef typename std::vector<int>::size_type size_type; 觉得这里面的typena ...
- POJ 1650 Integer Approximation
Integer Approximation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5081 Accepted: ...
- 逆转序列的递归/尾递归(+destructuring assignment)实现(JavaScript + ES6)
这里是用 JavaScript 做的逆转序列(数组/字符串)的递归/尾递归实现.另外还尝鲜用了一下 ES6 的destructuring assignment + spread operator 做了 ...
- PowerPoint 打开文档发现.pptx中胡内容有问题
一.问题的提出 有一个文件,在window 7操作系统中通过邮箱地址保存到本地,结果打开的时候出现[PowerPoint 打开文档发现 文件.pptx中胡内容有问题] 然后提示[如果您信任此演示文稿的 ...