您的位置:首页 > 产品设计 > UI/UE

共同学习Java源代码--常用工具类--AbstractStringBuilder(二)

2016-04-18 14:57 447 查看
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;

}

这个方法是设置可变字符串长度的方法。先判断参数,也就是要设置的长度是否小于零,小于零的话抛出异常。然后调用之前的方法扩大底层数组的长度。然后判断如果已有的字符数小于参数,那么就将扩容后的char数组value从count位到参数所在位置都填满\0也就是空字符。

public char charAt(int index) {

if ((index < 0) || (index >= count))

throw new StringIndexOutOfBoundsException(index);

return value[index];

}

这个方法比较简单就是返回指定下标的字符。

public int codePointAt(int index) {

if ((index < 0) || (index >= count)) {

throw new StringIndexOutOfBoundsException(index);

}

return Character.codePointAtImpl(value, index, count);

}

这个方法是返回指定下标的代码点。

public int codePointBefore(int index) {

int i = index - 1;

if ((i < 0) || (i >= count)) {

throw new StringIndexOutOfBoundsException(index);

}

return Character.codePointBeforeImpl(value, index, 0);

}

这是获取该元素之前那个元素的代码点。

public int codePointCount(int beginIndex, int endIndex) {

if (beginIndex < 0 || endIndex > count || beginIndex > endIndex) {

throw new IndexOutOfBoundsException();

}

return Character.codePointCountImpl(value, beginIndex, endIndex-beginIndex);

}

这是获取指定下标区间内代码点的数量。

public int offsetByCodePoints(int index, int codePointOffset) {

if (index < 0 || index > count) {

throw new IndexOutOfBoundsException();

}

return Character.offsetByCodePointsImpl(value, 0, count,

index, codePointOffset);

}

这个方法是返回从String对象指定索引开始,到另一个指定索引结束的区间内的代码点长度。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: