JAVA创建顺序串类SeqString的新建串
时间: 2024-05-09 13:21:26 浏览: 29
以下是JAVA创建顺序串类SeqString的新建串的示例代码:
```
public class SeqString {
private char[] data;
public SeqString() {
this.data = new char[0];
}
public SeqString(String str) {
this.data = str.toCharArray();
}
public SeqString(char[] charArray) {
this.data = charArray.clone();
}
public int length() {
return this.data.length;
}
public char charAt(int index) {
return this.data[index];
}
public SeqString substring(int beginIndex, int endIndex) {
char[] sub = new char[endIndex - beginIndex];
System.arraycopy(this.data, beginIndex, sub, 0, endIndex - beginIndex);
return new SeqString(sub);
}
public SeqString concat(SeqString str) {
char[] newData = new char[this.data.length + str.length()];
System.arraycopy(this.data, 0, newData, 0, this.data.length);
System.arraycopy(str.data, 0, newData, this.data.length, str.length());
return new SeqString(newData);
}
public boolean equals(SeqString str) {
if (this.data.length != str.length()) {
return false;
} else {
for (int i = 0; i < this.data.length; i++) {
if (this.data[i] != str.charAt(i)) {
return false;
}
}
return true;
}
}
public int indexOf(SeqString str) {
for (int i = 0; i <= this.data.length - str.length(); i++) {
if (this.substring(i, i + str.length()).equals(str)) {
return i;
}
}
return -1;
}
public static void main(String[] args) {
SeqString str1 = new SeqString();
SeqString str2 = new SeqString("Hello");
SeqString str3 = new SeqString(new char[] {'W', 'o', 'r', 'l', 'd'});
System.out.println(str1.length()); // 0
System.out.println(str2.charAt(1)); // e
System.out.println(str3.substring(1, 3).equals(new SeqString("or"))); // true
SeqString str4 = str2.concat(str3);
System.out.println(str4.equals(new SeqString("HelloWorld"))); // true
SeqString str5 = new SeqString("Hello");
SeqString str6 = new SeqString("World");
System.out.println(str5.equals(str6)); // false
System.out.println(str4.indexOf(str3)); // 5
}
}
```
在这个示例代码中,我们创建了一个SeqString类,实现了字符串的各种操作,包括新建空串、新建指定字符串的串、新建指定字符数组的串、获取字符串长度、获取指定位置字符、截取子串、串连接、判断相等、查找子串在主串中的位置等方法。在main方法中,我们对这些方法进行了测试。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)