Java语言程序设计与数据结构(基础篇)课后练习题 第十章(四)

10.22

class MyString1 {

private char[] s;
public MyString1(char[] chars) {
	this.s = chars;
}

public char charAt(int index) {
	if (index < s.length)
		return s[index];
	else {
		System.out.print("out!");
		return '@';
	}
}

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

public MyString1 substring(int begin, int end) {
	char[] s1 = new char[end - begin + 1];
	int a = 0;
	int b = 0;
	for (a = begin - 1; a < end; a++) {
		s[b] = s[a];
		b++;
	}
	MyString1 newString = new MyString1(s1);
	return newString;
}

public MyString1 toLowerCase() {
	String str = String.valueOf(s);
	str.toLowerCase();
	char ss[] = str.toCharArray();
	MyString1 newString = new MyString1(ss);
	return newString;
}

public boolean equals(MyString1 s) {
	return this.s.equals(s);
}

public static MyString1 valueOf(int i) {
	char[] c = new char[1];
	c[0] = (char) (i + (int) '0');
	MyString1 newString = new MyString1(c);
	return newString;
}

}

10.23

class MyString2 {

private String s;
public MyString2(String s) {
	this.s = s;
}

public int compare(String s) {
	return this.s.compareTo(s);// 返回的是差值
}

public MyString2 substring(int begin) {
	return new MyString2(s.substring(begin));
}

public MyString2 toUpperCase() {
	return new MyString2(s.toUpperCase());
}

public char[] toChars() {
	char[] a = s.toCharArray();
	return a;
}

public static MyString2 valueOf(boolean b) {
	if (b) {
		String str = "true";
		return new MyString2(str);
	} else {
		String str = "false";
		return new MyString2(str);
	}
}

}

10.24

class Character1 {

	private char c;
	public boolean isLetter(char ch){
		return Character.isLetter(ch);
	} 
	public static boolean isDigit(char ch){
		return Character.isDigit(ch);
	}
	public boolean isWhitespace(char ch){
		return Character.isWhitespace(ch);
	}
	public boolean isUpperCase(char ch){
		return Character.isUpperCase(ch);
	}
	public boolean isLowerCase(char ch){
		return Character.isLowerCase(ch);
	}
	public char toUpperCase(char ch){
		return Character.toUpperCase(ch);
	}
	public char toLowerCase(char ch){
		return Character.toLowerCase(ch);
	}
	public String toString(char ch){
		return Character.toString(ch);
	}

}

10.25

public class dishizhang {

public static void main(String[] args) {
    String[] s1 = split("ab#12#453", "#");
    String[] s2 = split("a?b?gf#e", "[?#]");
    printArray(s1);
    printArray(s2);
}

public static String[] split(String s, String regex) {
    String[] newStringArray = new String[s.length()-1];  
    int i = 0;  
    int j = 0;  
    while(i < s.length()){  
        if(s.substring(i, i+1).matches(regex)) {  
            if(i != 0) {
                newStringArray[j] = s.substring(0, i);
                j++;
            }  
            newStringArray[j] = s.substring(i, i+1);  
            j++;
            s=s.substring(i+1);  	
            } else
            i++;  
    }
    newStringArray[j] = s; 
    String[] trimedStringArray = new String[j+1]; 
    for(int n=0; n<trimedStringArray.length; n++) {
        if(newStringArray[n] != null)
            trimedStringArray[n] = newStringArray[n];
    }  
    return trimedStringArray;  
}

public static void printArray(String[] array) {
    for(int i=0; i<array.length; i++)
        System.out.print(array[i] + " ");
    System.out.println();
}

}

10.26

程序清单7-9 ????????

10.27

class MyStringBuilder1 {

private char[] e;
private int length;
public MyStringBuilder1(String s) {
	length = s.length();
	e = new char[s.length()];
	for (int i = 0; i < s.length(); i++)
		e[i] = s.charAt(i);
}

public MyStringBuilder1 append(MyStringBuilder1 s) {
	return new MyStringBuilder1(this.toString() + s.toString());
}

public MyStringBuilder1 append(int i) {
	return append(new MyStringBuilder1(i + ""));
}

public int length() {
	return length;
}

public char charAt(int index) {
	if (index < length)
		return e[index];
	else {
		System.out.print("out!");
		return '@';
	}
}

public MyStringBuilder1 toLowerCase() {
	return new MyStringBuilder1(this.toString().toLowerCase());
}

public MyStringBuilder1 substring(int begin, int end) {
	return new MyStringBuilder1(this.toString().substring(begin, end));
}

public String toString() {
	return new String(e);
}

}

10.28

class MyStringBuilder2 {

private String s;
public MyStringBuilder2() {
	s = new String();
}

public MyStringBuilder2(char[] chars) {
	s = new String(chars);
}

public MyStringBuilder2(String s) {
	s = "";
	for (int i = 0; i < s.length(); i++)
		s += s.charAt(i);
}

public MyStringBuilder2 insert(int offset, MyStringBuilder2 s) {
	MyStringBuilder2 s2 = new MyStringBuilder2(
			this.toString().substring(0, offset) + s.toString() + this.toString().substring(offset));
	return s2;
}

public MyStringBuilder2 reverse() {
	String s3 = "";
	int length = s.length();
	for (int i = 0; i < this.s.length(); i++)
		s3 += s.charAt(length - 1 - i);
	return new MyStringBuilder2(s3);
}

public MyStringBuilder2 substring(int begin) {
	return new MyStringBuilder2(this.toString().substring(begin));
}

public MyStringBuilder2 toUpperCase() {
	return new MyStringBuilder2(this.toString().toUpperCase());
}

public String toString() {
	return s;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xupengboo

你的鼓励将是我创作最大的动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值