自己实现一个StringBuffer

redpigX 2019-03-13

java 源码 pos string void

自己动手实现了一个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);
    }
}
登录 后评论
下一篇
冒顿单于
9870人浏览
2019-08-28
相关推荐
Java基础巩固计划
637人浏览
2018-03-27 21:20:00
Java基础部分3
508人浏览
2017-11-15 18:35:00
java笔试题(1)
515人浏览
2017-08-28 16:42:00
11.1.1 认识StringBuffer类(1)
595人浏览
2017-11-09 00:23:00
字符串的翻转实现
326人浏览
2017-09-25 11:13:00
Java性能优化的50个细节
5073人浏览
2018-11-27 10:17:30
0
0
0
529