数据结构之顺序串

1. 与链串的对比(待完善…)

2. 代码实现

2.1 定义规范

import java.util.Arrays;

/**
 * @author LiuYun
 * @version 1.0
 */
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];
     }

     /**
      * 截取字串
      * @param begin  开始索引(包含)
      * @param end    结束索引(不包含)
      * @return 字串
      */
     public abstract BaseSeqString substring(int begin, int end);

     /**
      * 插入字符串
      * @param offset  插入位置
      * @param str     子串
      * @return        结果串
      */
     public abstract BaseSeqString insert(int offset, BaseSeqString str);

     /**
      * 删除串
      * @param begin   开始位置(包含)
      * @param end     结束位置(不包含)
      * @return        结果串
      */
     public abstract BaseSeqString delete(int begin, int end);

     /**
      * 连接串
      * @param str     子串
      * @return        结果串
      */
     public abstract BaseSeqString concat(BaseSeqString str);

     /**
      * 串比较
      * @param str     用来比较的串
      * @return        大于 > 1, 小于 < 1, 等于 = 1
      */
     public abstract int compareTo(BaseSeqString str);

     /**
      * 子串定位
      * @param begin   起始索引
      * @param str     字串
      * @return        主串中子串的起始索引
      */
     public abstract int indexOf(int begin, BaseSeqString str);
}

2.2 实现规范(待完善…)

import java.util.Arrays;

/**
 * @author LiuYun
 * @version 1.0
 */
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;

/**
 * @author LiuYun
 * @version 1.0
 */
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();
    }
}
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值