本文共 3879 字,大约阅读时间需要 12 分钟。
自己动手实现了一个StringBuffer,算是个玩具代码,各种边界问题都没有做检查,但是对于理解StringBuffer来说,足够了。
实现过程中,查看了许多源码,弄明白了很多概念,其中还有一个问题是关于System.arraycopy()做删除操作,这篇算是未完待续吧!package character;import java.util.Arrays;public class IStringBuffer { private char[] value; private int count; //default no-arg constructor public MyStringBuffer() { value = new char[16]; } //constructor with capacity public MyStringBuffer(int capacity) { value = new char[capacity]; } //constructor with string public MyStringBuffer(String str) { value = new char[str.length() + 16]; append(str); } public static void main(String[] args) { MyStringBuffer msb = new MyStringBuffer("the"); msb.append(" redpig is writting java programs for fun"); System.out.println("msb is: " + msb); System.out.println("length is: " + msb.length()); System.out.println("capacity is: " + msb.capacity()); msb.append('!'); System.out.println("msb is: " + msb); System.out.println("length is: " + msb.length()); System.out.println("capacity is: " + msb.capacity()); msb.insert(23, "funny "); System.out.println("msb is: " + msb); System.out.println("length is: " + msb.length()); System.out.println("capacity is: " + msb.capacity()); msb.insert(23, ' '); msb.insert(23, 'a'); System.out.println("msb is: " + msb); System.out.println("length is: " + msb.length()); System.out.println("capacity is: " + msb.capacity()); msb.delete(23, 31); System.out.println("msb is: " + msb); System.out.println("length is: " + msb.length()); System.out.println("capacity is: " + msb.capacity()); msb.delete(36); System.out.println("msb is: " + msb); System.out.println("length is: " + msb.length()); System.out.println("capacity is: " + msb.capacity()); msb.reverse(); System.out.println("msb is: " + msb); System.out.println("length is: " + msb.length()); System.out.println("capacity is: " + msb.capacity()); } //追加字符串 public void append(String str) { int newCapacity; if ((str.length() + count) - value.length > 0) { newCapacity = (value.length << 1) + 2; if (newCapacity - (str.length() + count) < 0) newCapacity = str.length() + count; value = Arrays.copyOf(value, newCapacity); } str.getChars(0, str.length(), value, count); count += str.length(); } //追加字符 public void append(char c) { this.append(String.valueOf(c)); } public void insert(int pos, String str) { if ((str.length() + count) - value.length > 0) { int newCapacity; newCapacity = (value.length << 1) + 2; if (newCapacity - (str.length() + count) < 0) newCapacity = str.length() + count; value = Arrays.copyOf(value, newCapacity); } System.arraycopy(value, pos, value, pos + str.length(), count - pos); str.getChars(0, str.length(), value, pos); count += str.length(); } public void insert(int pos, char c) { this.insert(pos, String.valueOf(c)); } public void delete(int start, int end) { System.arraycopy(value, end, value, start, capacity() - end); count -= (end - start); } public void delete(int start) { this.delete(start, count); } public void reverse() { int n = count - 1; for (int j = (n - 1) >> 1; j >= 0; j--) { int k = n - j; char cj = value[j]; char ck = value[k]; value[j] = ck; value[k] = cj; } } public int length() { return count; } public int capacity() { return value.length; } public String toString() { return String.valueOf(value); }}
转载地址:http://jqtwm.baihongyu.com/