您现在的位置是:首页 > 文章详情

自己实现一个StringBuffer

日期:2019-03-12点击:552

自己动手实现了一个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); } }
原文链接:https://yq.aliyun.com/articles/693472
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章