自己实现一个StringBuffer

本文深入探讨了自制StringBuffer的实现细节,包括构造方法、追加、插入、删除、反转等核心功能,通过具体代码示例展示了如何管理和调整容量,以及如何进行字符串操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自己动手实现了一个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);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值