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 了
}
}
这个就是测试的结果哦