Java Implement the StringBuilder class

The String class is provided in the Java library. Provide your ownimplementation for the following methods (name the new class MyString1 ):

public MyString1(char[] chars);
public char charAt(int index);
public int length();
public MyString1 substring(int begin, intend);
public MyString1 toLowerCase();
public boolean equals(MyString1 s);

这里的class名为 MyString_10_22 哦

public class MyString_10_22 {

    private  char[] stringc;

    public MyString_10_22(char[] chars) {
        stringc = chars;
    }

    public void showMyString() {
        for (int i = 0; i < stringc.length; i++)
            System.out.print(stringc[i]);
        System.out.println();
    }

    public char charAt(int index) {
        return stringc[index];
    }

    public int length() {
        return stringc.length;
    }

    public MyString_10_22 substring(int begin, int end) {
        char[] sub = new char[end - begin];
        for(int i = begin; i < end; i++) {
            sub[i - begin] = stringc[i];
        }

        MyString_10_22 subs = new MyString_10_22(sub);

        return subs;
    }

    public MyString_10_22 toLowerCase() {
        char[] s1 = new char[stringc.length];
        for (int i = 0; i < stringc.length; i++) {
            if (Character.isUpperCase(stringc[i]))
                s1[i] = Character.toLowerCase(stringc[i]);
            else
                s1[i] = stringc[i];
        }

        MyString_10_22 s2 = new MyString_10_22(s1);

        return s2;
    }

    public boolean equals(MyString_10_22 s) {
       int n = s.length();
        for (int i = 0; i < n; i++) {
            if((this.stringc)[i] != s.charAt(i))
                return false;
        }

        return true;
    }

    public static MyString_10_22 valueOf(int i) {
        char[] s1 = new char[1];
        s1[0] = (char)i;
        MyString_10_22 s2 = new MyString_10_22(s1);
        return s2;

    }


}

下面我们来设置一个 test class 来测试一下吧

public class Test {
    public static void main(String[] args) {
        char[] c = {'h', 'e', 'l', 'l', 'o'};
        MyString_10_22 stre = new MyString_10_22(c);
        stre.showMyString();
        //注意一下,第7行 不能写成  System.out.println(stre.showMyString());
        //因为 在方法 showMyString中就已经有 System.out.println 了
        
    }
}

这个就是测试的结果哦
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值