数据结构-顺序表

实现顺序表的多种方法

创建一个顺序表的包

实现main函数和IList接口

在IList接口中写出所有方法:

public interface IList {
    //判断顺序表元素是否满了
    boolean isFull();
    //判断顺序表是否为空
    boolean isEmpty();
    //打印顺序表
    void display();
    //新增元素,默认在数组最后新增
    void add (int data);
    //在pos位置新增元素
    void add(int pos, int data);
    //判断是否包含某个元素
    public boolean contains(int toFind);
    //查找某个元素对应的位置
    public int indexOf(int toFind);
    //获取pos位置的元素
    public int get(int pos);
    //给pos位置的元素设置为value
    public void set(int pos, int value);
    //删除第一次出现的关键字key
    public void remove(int toRemove);
    //获取顺序表长度
    public int size();
    //清空顺序表
    public void clear();
}

用MyArrayList实现这个接口然后重写里面所有的方法:

 

public class MyArrayList implements IList {
    public int[] elem;
    public int usedSize;

    public static final int DEFAULT_SIZE = 10;

    //构造方法去初始化成员
    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    public MyArrayList(int capacity) {
        this.elem = new int[capacity];
    }

    private void checkCapacity() {
        if (isFull()) {
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
    }

    private void checkPosOnAdd(int pos) throws PosIllegality {
        if (pos < 0 || pos > usedSize) {
            System.out.println("不是合法插入位置!");
            //抛异常
            throw new PosIllegality("插入元素下标异常!" + pos);
        }
    }

    @Override
    public boolean isEmpty() {
        return usedSize == 0;
    }

    //打印
    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(this.elem[i] + " ");
        }
        System.out.println();
    }

    @Override
    public boolean isFull() {
        return usedSize == elem.length;
    }

    //顺序表尾插
    @Override
    public void add(int data) {
        checkCapacity();
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    @Override
    public void add(int pos, int data) {
        try {
            checkPosOnAdd(pos);
        } catch (PosIllegality e) { //捕获异常
            e.printStackTrace();
            return;
        }
        checkCapacity();

        //1.从最后一个有效的数据开始往后移动
        for (int i = usedSize - 1; i >= pos; i--) {
            elem[i + 1] = elem[i];
        }
        //2.当i<pos就结束
        //3.存放元素到pos位置
        elem[pos] = data;
        //4.usedSize++;
        usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
        if(isEmpty()) {
            return false;
        }
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {//如果找的是引用类型怎么判断?重写equals方法
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if(isEmpty()) {
            return -1;
        }
        for (int i = 0; i < usedSize; i++) {
            if(elem[i] == toFind) {//如果找的是引用类型怎么判断?重写equals方法
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        checkPosOnGetAndSet(pos);
        if (isEmpty()) {
            throw new MyArrayListEmpty("获取指定下标元素时顺序表为空!");
        }
        return elem[pos];
    }
    private void checkPosOnGetAndSet(int pos) throws PosIllegality {
        if (pos < 0 || pos >= usedSize) {
            System.out.println("获取指定下标的元素异常!");
            //抛异常
            throw new PosIllegality("插入元素下标异常!" + pos);
        }
    }

    @Override
    public void set(int pos, int value) {
        checkPosOnGetAndSet(pos);
        elem[pos] = value;
    }

    @Override
    public void remove(int toRemove) {
        //1.找到要删除的数字
        int index = indexOf(toRemove);
        if(index == -1) {
            System.out.println("没有这个数字!");
            return;
        }
        for (int i = index; i < usedSize-2; i++) {
            elem[i] = elem[i+1];//注意越界
        }
        usedSize--;
        //2.挪动数据
        //3.修改size
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {

    }
}

其中,抛异常代码为:

public class MyArrayListEmpty extends RuntimeException {
    public MyArrayListEmpty(String msg) {
        super(msg);
    }
}
public class PosIllegality extends RuntimeException {
    public PosIllegality(String msg) {
        super(msg);
    }
}

其中RuntimeException这个类的方法如下:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值