【第十三天】

String Butter

字符常用类
String Butter自动生成一组数组,不同jdk不一样,我的一共18个字符,它生成长度为26的数组。
最后两句查看
public class TestString {
    public static void main(String args[]){
        String str1 = "let there ";
        StringBuffer sb = new StringBuffer(str1);   //根据str1 创建一个StringButter对象
        sb.append("be light");  //在最后追加
        System.out.println(sb);
        sb.delete(4,10);    //删除4-10字符
        System.out.println(sb);
        sb.insert(4,"there ");  //第四个字符后插入there
        System.out.println(sb);
        sb.reverse();       //反转字符
        System.out.println(sb);

        System.out.println(sb.length());
        System.out.println(sb.capacity());
    }
}

接口IStringButter有参构造(有错误)

public interface IStringBuffer {
    public void append(String str); //追加字符串
    public void append(char c);  //追加字符
    public void insert(int pos,char b); //指定位置插入字符
    public void insert(int pos,String b); //指定位置插入字符串
    public void delete(int start); //从开始位置删除剩下的
    public void delete(int start,int end); //从开始位置删除结束位置-1
    public void reverse(); //反转
    public int length(); //返回长度
}
public class MyStringButter implements IStringBuffer{
    int capacity = 16;
    int length = 0;
    char[] value;

    public MyStringButter(){
        value = new char[capacity];
    }
    public MyStringButter(String str){
        if(null!=str)
            value = str.toCharArray();
        length = value.length;

        if(capacity< value.length){
            capacity = value.length*2;
        }
    }

    public void append(String str){
        //todo auto-generated method stub
    }
    public void append(char c){

    }

    @Override
    public void insert(int pos, char b) {

    }

    public void delete(int start){

    }

    @Override
        public void delete(int start, int end) {

    }

    public void reverse(){
        for (int i=0;i<length/2;i++){
            char temp = value[i];
            value[i]=value[length-i-1];
            value[length-i-1]=temp;
        }
    }
    public int length(){
        return 0;
    }
//    public void insert(int pos, char b){
//        //boundary condition judgment
//        if(pos<0)
//            return;
//        if(pos>length)
//            return;
//        if(pos == b)
//            return;
//        //dilatation
//        while ((length + b)>capacity){
//            capacity =(int)((length+b)*1.5f);
//            char[] newValue = new char[capacity];
//            System.arraycopy(value,0,newValue,0,length);
//            value = newValue;
//        }
//        char[] cs = b.toCharArray()
//    }

    @Override
    public void insert(int pos, String b) {

    }

    public static void main(String args[]){
        MyStringButter sb = new MyStringButter("there light") {
            @Override
            public void insert(int pos, String b) {

            }
        };

        sb.reverse();
        System.out.println(sb);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值