自己实现一个StringBuffer

简介: 自己动手实现了一个StringBuffer,算是个玩具代码,各种边界问题都没有做检查,但是对于理解StringBuffer来说,足够了。实现过程中,查看了许多源码,弄明白了很多概念,其中还有一个问题是关于System.

自己动手实现了一个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);
    }
}
目录
相关文章
|
XML Java 数据库
Flowable入门程序——构建一个命令行程序(一)
Flowable入门程序——构建一个命令行程序
340 0
|
4月前
|
人工智能 自然语言处理 数据可视化
AI 助手带你玩转数据分析!通义灵码保姆级教学 | 共学课2期上线
7月15日20:00,通义灵码联合WaytoAGI社区推出《AI助手带你玩转数据分析》公开课。零门槛、零代码,只需中文指令,即可完成数据读取、分析到报告生成全流程。告别代码恐惧,业务人员也能轻松掌握数据分析,提升职场竞争力。
205 0
|
27天前
|
Java API 开发工具
百宝箱开放平台 ✖️ Java SDK
百宝箱提供Java SDK,支持开发者集成其开放能力。需先发布应用,准备Java 8+及Maven环境,通过添加依赖安装SDK,并初始化客户端调用对话型或生成型智能体,实现会话管理、消息查询与文件上传等功能。
1222 0
百宝箱开放平台 ✖️ Java SDK
|
4月前
|
人工智能 Android开发 iOS开发
安卓版快捷指令,加了AI语音可以一句话操作v0.2.7
Shortcuts for Android(SFA)是一款安卓自动化工具,支持语音创建快捷指令,实现听歌、导航、发消息等操作。操作简单,提升效率,快来体验语音控制的便捷!
706 0
安卓版快捷指令,加了AI语音可以一句话操作v0.2.7
|
9月前
|
网络安全 SEO
如何简单建设一个网站?
在建站时,使用建站平台搭建简单,明确目标与定位、选择域名与主机域名、部署和运行将模板上传,完善网站信息,优化和维护网站后,考虑SEO和后续维护。
299 2
|
8月前
|
数据可视化 数据挖掘 Java
报表工具怎么选?8款主流报表工具大测评!
报表工具怎么选?8款主流报表工具大测评!
|
Web App开发 前端开发 测试技术
Xpath Helper 在新版Edge中的安装及解决快捷键冲突问题
Xpath Helper 在新版Edge中的安装及解决快捷键冲突问题
882 0
|
弹性计算 运维 Cloud Native
长虹佳华章宇:为计算巢入驻ISV提供软件分销能力
点击直达长虹佳华店铺:https://market.aliyun.com/store/4925812/index.html | 长虹佳华云生态商业部总经理章宇在【云栖大会计算巢专场】发表了题为《长虹佳华为计算巢入驻ISV提供软件分销能力》的主题分享,为大家介绍了长虹佳华的发展历程,以及亿氪虹云如何提供SaaS加速服务和SaaS分销在计算巢的最佳实践。
PPT - 如何在PPT里面实现数字的随机抽奖?
PPT - 如何在PPT里面实现数字的随机抽奖?
903 0
PPT - 如何在PPT里面实现数字的随机抽奖?
|
程序员 Python
利用Python实现科学式占卜
一直以来,中式占卜都是基于算命先生手工实现,程序繁琐(往往需要沐浴、计算天时、静心等等流程)。准备工作复杂(通常需要铜钱等道具),计算方法复杂,需要纯手工计算二进制并转换为最终的卦象,为了解决这个问题,笔者基于python实现了一套科学算命工具,用于快速进行占卜。 本文的算命方式采用八卦 + 周易+ 梅花易数实现,脚本基于python3.9.0开发。本人对于周易五行研究较浅,如有疏漏请见谅。 最终效果如图,在运行程序之后,会根据当前的运势自动获取你心中所想之事的卦象(本卦、互卦、变卦) 前置知识 基础原理 首先我们需要了解一些最基本的占卜知识,目前我国几种比较主流的占卜方式基本都是基
326 0