数据结构变量字符串类(JAVA)

数据结构变量字符串类(JAVA)

package Chapter3;

import java.io.Serializable;

public class MyStringBuffer implements Serializable {
    private char[] value;   //字符数组,私有成员变量
    private int n;          //串长度

    /*构造容量为capacity的空串*/
    public MyStringBuffer(int capacity){
        this.value=new char[capacity];
        this.n=0;
    }
    /*以默认容量构造空串*/
    public MyStringBuffer(){
        this(16);
    }
    /*以字符串常量构造串*/
    public MyStringBuffer(String s){
        this(s.length()+16);
        this.n=s.length();
        for (int i=0; i<this.n; i++){
            this.value[i]=s.charAt(i);
        }
    }
    /*返回字符串长度*/
    public int length(){
        return this.n;
    }
    /*返回字符串数组容量*/
    public int capacity(){
        return this.value.length;
    }

    public synchronized String toString(){
        return new String(this.value, 0, this.n);
    }

    /*返回第i个字符*/
    public synchronized char charAt(int i){
        if (i < 0){
            i=0;
        }
        else if (i > this.n){
            i=this.n;
        }
        return this.value[i];
    }
    /*设置第i个字符为ch*/
    public void setCharAt(int i, char ch){
        if (i < 0){
            i=0;
        }
        else if (i > this.n){
            i=this.n;
        }
        this.value[i]=ch;
    }

    /*在第i个字符处插入s串*/
    public synchronized MyStringBuffer insert(int i, String s){
        if (this.n==0 && i==0 || this.n>0 && i>=0 && i<=this.n){
            if (s==null){
                s="";
            }
            char[] temp=this.value;
            //若数组空间不足,则扩充
            if (this.value.length <this.n + s.length()){
                this.value=new char[(this.value.length + s.length()) * 2];
                for (int j=0; j<i; j++){
                    this.value[j]=temp[j];
                }
            }
            for (int j=this.n-1; j>=i; j--){
                this.value[j + s.length()]=temp[j];
            }
            for (int j=0; j<s.length(); j++){
                this.value[i+j]=s.charAt(j);
            }
            this.n += s.length();
            return this;
        }
        else {
            throw new StringIndexOutOfBoundsException("i=" + i);
        }
    }
    public synchronized MyStringBuffer append(String s){
        return this.insert(this.n,s);
    }

    /*删除从begin到end-1的子串*/
    public synchronized MyStringBuffer delete(int begin, int end){
        if (begin>=0 && begin<this.n && end>=0 && begin<=end){
            if (end > this.n){
                end=this.n;
            }
            for (int i=0; i<this.n-end; i++){
                this.value[begin+i]=this.value[end+i];
            }
            this.n -= end-begin;
            return this;
        }
        else {
            throw new StringIndexOutOfBoundsException();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值