顺序存储结构的串(数据结构)

public class string {
    int maxSize = 10;
    private char[] chars;
    private int length;
    public string(){}
    public string(int n){//构造一个能保存n个字符的串
        this.maxSize = n;
        this.chars = new char[n];
        this.length = 0;
    }

    public void copy(string t){//将串t复制给当前串

        if(this.maxSize<t.maxSize){//无法容纳t则扩充长度
            this.maxSize = t.maxSize;
            this.chars = new char[this.maxSize];
        }

        this.length=0;//初始化当前串
        for(int i=0;i<t.getLength();i++){
            this.chars[i] = t.chars[i];
            this.length++;
        }
    }
    public boolean isEmpty(){

    }

    public int compare(string t){//将当前串与t比较
        int i = 0;
        while (this.chars[i]==t.chars[i] && i<this.length && i<t.getLength()){
            i++;
        }
        if(i==this.length && i==t.length){
            return 0;
        } else if (i ==t.getLength() && i<this.length) {
            return 1;
        }
        else return -1;
    }

    public int getLength(){

    }
    public int getLength(){}

    public boolean clear(){}
    public void concat(string t){//将指定串t连接到当前串中
        if(this.maxSize<this.length+t.getLength()){//当前串无法容纳,则先扩充长度

            char[] a = new char[this.length];//用a[]暂存串的内容
            for(int i=0;i<this.length;i++){
                a[i] = this.chars[i];
            }

            //扩充当前串的长度
            this.maxSize = this.length+t.getLength();
            this.chars = new char[this.maxSize];

            //恢复当前串的原始状态
            for(int i = 0;i<a.length;i++){
                this.chars[i] = a[i];
            }
        }

        //将串t的内容添加到串的尾部
        for(int i=0;i<t.getLength();i++){
            this.chars[this.length] = t.chars[i];
            this.length++;
        }
    }

    public string subString(int pos,int len){//将当前串中第pos个字符开始提取len个字符,得到一个子串
        if(pos+len>=this.length){//若提取的子串起始位置与提取子串的长度之和超过当前串,则操作异常并返回空串
            return null;
        }

        string a = new string(len);
        for(int i = 0;i<len;i++){
            a.chars[i] = this.chars[pos+i];
            a.length++;
        }
        return a;
    }

    public string subString(int pos) {//将当前串中第pos个字符直到最后一个字符提取出来得到一个子串

    }

    public int index(string t){//返回字符串t在当前串中首次出现的位置,若不存在,返回-1

        if(this.length<t.getLength())//串t长度大于当前串长度,则直接返回-1
            return -1;

        int a = -1;//设置标志变量a,其默认值表示不存在串t
        for(int i=0;i<this.length;i++){//从左往右查找串t
            int j = 0;
            while (j<t.getLength() && this.chars[i+j]==t.chars[j]){
                if(this.chars[i+j]!=t.chars[j]) break;
                j++;
            }

            if(j==t.getLength()){//找到串t,记录其起始位置
                a=i;
                break;
            }
        }
        return a;
    }

    public int lastIndex(string t){//返回字符串t最后一次出现的位置,不存在返回-1

    }

    public int replace(string t,string v){//用串v替换所有与串t相同的子串,返回替换次数

    }

    public boolean insert(string t,int pos){//将串t插入到当前串pos处

    }

    public boolean delete(int pos,int n){//删除从pos开始连续n个字符

    }

    public boolean remove(string t){//移除与t所有相同的子串,返回删除次数

    }

    public void toUpperCse(){//将所有字母转换为大写

    }

    public void toLowerCse(){//将所有字符转换为小写

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值