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 ...
随机推荐
- IOS , plist 配置项说明
本文转载至 http://blog.csdn.net/fengsh998/article/details/8307424 Key:Application can be killed immediate ...
- MD5 加密的两种方法
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5Cryp ...
- 使用pathogen管理Vim插件并托管到Github
参照文章[1][2]的办法,将vim打造成一个Python开发环境.文章中使用的是 pathogen + git 来管理 Vim 插件的.对这种方式还不太明白的同学可以参考[3]中的介绍.pathog ...
- synchronized关键字,Lock接口以及可重入锁ReentrantLock
多线程环境下,必须考虑线程同步的问题,这是因为多个线程同时访问变量或者资源时会有线程争用,比如A线程读取了一个变量,B线程也读取了这个变量,然后他们同时对这个变量做了修改,写回到内存中,由于是同时做修 ...
- Domain Space
Bluehost Register Page http://www.bluehost.com/track/weipengp
- 读书笔记_Effective_C++_条款四十四:将与参数无关的代码抽离template
标题上说“将与参数无关的代码抽离template”,这里的参数既可以指类型,也可以是非类型,我们先来看看非类型的情况. 假定我们要为矩阵写一个类,这个矩阵的行列元素个数相等,是一个方阵,因而我们可以对 ...
- 【PRML读书笔记-Chapter1-Introduction】1.6 Information Theory
熵 给定一个离散变量,我们观察它的每一个取值所包含的信息量的大小,因此,我们用来表示信息量的大小,概率分布为.当p(x)=1时,说明这个事件一定会发生,因此,它带给我的信息为0.(因为一定会发生,毫无 ...
- zepto - push
var arr = ['1', '2', '3', '4']; arr.push('qwe'); console.log(arr);
- css省略号布局实例截图
过多文字li标签出现使用css省略号样式截图 使用text-overflow样式让显示不完内容通过css实现省略号排版
- Gamma Gamma~!!!
左图是没有进行gamma矫正的,右图是进行了gamma矫正的.以前一直以为是Tone Map的公式计算有问题,后来看PBR的paper时候,终于明白了gamma的重要性,一改,果然发现颜色不想以前那么 ...
