java(数组实现)线性表中顺序表

package Xianxingbiao;
//线性表中的顺序表


interface List {//接口


    public void insert(int i, Object obj) throws Exception;


    public Object delete(int i) throws Exception;


    public Object getData(int i) throws Exception;


    public int size();


    public int locate(Object obj);


    public boolean isEmpty();


}


public class SeqList implements List {


    Object[] listArray;
    int size;                     //当前线性表元素个数
    int maxsize;                  //线性表能容纳的元素的个数
    final int defaultSize = 10;    //线性表默认的元素个数


    public SeqList() {           //数据初始化
        maxsize = defaultSize;
        size = 0;
        listArray = new Object[defaultSize];
    }


    public SeqList(int size) {  //人为的控制大小
        maxsize = size;          //最大容量
        this.size = 0;           // 当前大小
        listArray = new Object[size];
    }


    //插入操作
    public void insert(int i, Object obj) throws Exception {
        if (size == maxsize) {//队伍占满无法插入
            throw new Exception("顺序表已满,无法插入!");
        }
        if (i < 0 || i > size) {
            throw new Exception("参数错误");
        }
        for (int j = size; j > i; j--) {
            listArray[j] = listArray[j - 1];
        }
        listArray[i] = obj;
        size++;
    }


    public Object delete(int i) throws Exception {
        if (isEmpty()) {
            throw new Exception("顺序表为空无法删除!");
        }
        if (i < 0 || i > size) {
            throw new Exception("参数错误");
        }
        //画图更好理解
        for (int j = i + 1; j <= size - 1; j++) {
            listArray[j - 1] = listArray[j];
        }
        size--;
        return listArray[i];
    }


    public Object getData(int i) throws Exception {
        if (i < 0 || i > size) {
            throw new Exception("参数错误!");
        }
        if (!isEmpty()) {
            throw new Exception("顺序表为空!");
        }
        return listArray[i];
    }


    public int size() {
        return size;   //因为不存在歧义所以可以返回时不需要this
    }


    public int locate(Object obj) {
        int temp = 0;
        for (int i = 0; i < size; i++) {
            if (listArray[i] == obj) {
                temp = i;
                // break;  返回第一个满足条件的位置
            } else {
                temp = -1;
            }
        }
        if (temp == -1) {
            System.out.println("没有搜索到值,结果返回:\"-1\"!");
        }
        return temp;
    }


    public boolean isEmpty() {
        return this.size == 0;
    }


    public static void main(String[] args) throws Exception {
        SeqList list = new SeqList();
        list.insert(0, 5);
        list.insert(1, 19);
        list.insert(2, 19);
        list.isEmpty();
        System.out.println(list.delete(3));
        System.out.println(list.size());
        System.out.println(list.locate(19));
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

bruce_suxin

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

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

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

打赏作者

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

抵扣说明:

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

余额充值