1. 与链串的对比(待完善…)
2. 代码实现
2.1 定义规范
import java.util.Arrays;
public abstract class BaseSeqString {
protected char[] chars;
public BaseSeqString(char[] chars) {
this.chars = Arrays.copyOf(chars, chars.length);
}
public void clear() {
Arrays.fill(chars, '\u0000');
}
public boolean isEmpty() {
return this.chars.length == 0;
}
public int length() {
return this.chars.length;
}
public char charAt(int index) {
return this.chars[index];
}
public abstract BaseSeqString substring(int begin, int end);
public abstract BaseSeqString insert(int offset, BaseSeqString str);
public abstract BaseSeqString delete(int begin, int end);
public abstract BaseSeqString concat(BaseSeqString str);
public abstract int compareTo(BaseSeqString str);
public abstract int indexOf(int begin, BaseSeqString str);
}
2.2 实现规范(待完善…)
import java.util.Arrays;
public class SeqString extends BaseSeqString {
public SeqString(char[] chars) {
super(chars);
}
@Override
public SeqString substring(int begin, int end) {
if (begin < 0 || end > this.length()) {
throw new StringIndexOutOfBoundsException("索引越界");
}
if (begin > end) {
throw new StringIndexOutOfBoundsException("开始索引大于结束索引");
}
if (begin == end) {
return new SeqString(new char[0]);
}
char[] result = new char[end - begin];
for (int i = begin, x = 0; i < end; i++) {
result[x++] = this.charAt(i);
}
return new SeqString(result);
}
@Override
public SeqString insert(int offset, BaseSeqString str) {
if (offset < 0 || offset > this.length()) {
throw new StringIndexOutOfBoundsException("索引越界");
}
if (str.length() == 0) {
return this;
}
char[] chars = Arrays.copyOf(this.chars, this.length() + str.length());
System.arraycopy(this.chars, offset, chars, offset + str.length(), this.length() - offset);
System.arraycopy(str.chars, 0, chars, offset, str.length());
return new SeqString(chars);
}
@Override
public SeqString delete(int begin, int end) {
if (end > this.length() || begin < 0) {
throw new StringIndexOutOfBoundsException("索引越界");
}
if (begin > end) {
throw new StringIndexOutOfBoundsException("起始索引大于结束索引");
}
char[] chars = Arrays.copyOf(this.chars, this.chars.length);
for (int i = 0; i < chars.length - end; i++) {
chars[begin + i] = chars[end + i];
}
chars = Arrays.copyOf(chars, this.chars.length - (end - begin));
return new SeqString(chars);
}
@Override
public SeqString concat(BaseSeqString str) {
return null;
}
@Override
public int compareTo(BaseSeqString str) {
return 0;
}
@Override
public int indexOf(int begin, BaseSeqString str) {
return 0;
}
public void display() {
StringBuilder sb = new StringBuilder();
for (char e : this.chars) {
sb.append(e);
}
System.out.println(sb);
}
}
3. 测试代码(待完善…)
import org.junit.Before;
import org.junit.Test;
public class SeqStringTest {
private SeqString seqString;
@Before
public void init() {
char[] chars = new char[]{'a', 'b', 'c', 'd', 'e'};
seqString = new SeqString(chars);
}
@Test
public void substring() {
seqString.substring(5, 5).display();
}
@Test
public void insert() {
SeqString sub = new SeqString(new char[]{'1', '2', '3'});
seqString.insert(1, sub).display();
}
@Test
public void delete() {
seqString.delete(1, 3).display();
}
}