保安日记之顺序表

集合框架中用ArrayList和Vector来实现顺序表这个数据结构,他们的底层都是基于数组来实现的线性表,这两顺序表的区别在于:

  1. Vector是一个老版本的顺序表 ArrayList是新版本的顺序表
  2. Vector是线程安全的(效率比线程不安全版本低).ArrayList是线程安全的,不加锁的
  3. 现在不太推荐使用Vector.更建议使用JUC中的组件,优化的更好,性能更高

顺序表的特性就是知道我放了几个元素 并且不能跳着放

接下来我们就借助数组来自己实现一个顺序表:

public class MyArrayList {
    private int[] elem;//null
    private int usedSize;//0
    //设置默认容量
    private static final int DEFAULT_SIZE=4;

    public MyArrayList(){
        this.elem=new int[DEFAULT_SIZE];
        this.usedSize=0;
    }
    // 打印顺序表
    public void display() {
        for(int i=0;i<this.usedSize;i++){
            System.out.print(this.elem[i]+" ");
        }
    }
    // 在 pos 位置新增元素
    //1.判断当前顺序表是不是满的 
    //2.挪数据从顺序表的最后一个元素开始往后挪.一直挪到POS位置
    //在usedSize-1的位置开始挪位置,一直到POS位置
    public void add(int pos, int data) {
        if(isFull()){
            //System.out.println("顺序表是满的!");
            //return;
            grow();
        }
        if(pos<0||pos>this.usedSize){
            //System.out.println("pos位置不合法");
            //return;
            //抛出异常也可以
            throw new ArrayIndexOutOfBoundsException("pos位置不合法");
        }
        int i=this.usedSize-1;
        while(i>=pos){
            this.elem[i+1]=this.elem[i];
            i--;
        }
        this.elem[pos]=data;
        this.usedSize+=1;
    }
    //判断是不是满的
    public boolean isFull(){
        if(this.usedSize==this.elem.length){
            return true;
        }
        return false;
    }
    //判断顺序表是否为空
    public boolean isEmpty(){
        if(this.usedSize==0){
            return true;
        }
        return false;
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        if (isEmpty()) {
            //System.out.println("顺序表为空!");
            //return false;
            throw new RuntimeException("顺序表为空!");
        }
        for (int i = 0; i < this.usedSize; i++) {
            if (toFind == this.elem[i]) {
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int search(int toFind) {
        if (isEmpty()) {
            //System.out.println("顺序表为空!");
            //return false;
            throw new RuntimeException("顺序表为空!");
        }
        for (int i = 0; i < this.usedSize; i++) {
            if (toFind == this.elem[i]) {
                return i;
            }
        }
        return -1;
    }

    // 获取 pos 位置的元素
    public int getPos(int pos) {
        if(isEmpty()){
            throw new RuntimeException("顺序表为空!");
        }
        if(pos<0||pos>this.usedSize){
            throw new ArrayIndexOutOfBoundsException("pos位置不合法");
        }
        return this.elem[pos];
         }
    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) { }
    //删除第一次出现的关键字key
    public void remove(int key) {
        if(key<0||key>=this.usedSize){
            throw new ArrayIndexOutOfBoundsException();
        }
        if(search(key)==-1){
            return;
        }
        int i=search(key);
        while(i<this.usedSize-1){
            this.elem[i]=this.elem[i+1];
            i++;
        }
        this.usedSize-=1;

    }
    // 获取顺序表长度
    public int size() {
        return this.usedSize; }
    // 清空顺序表
    //置为0
    public void clear() {
        this.usedSize=0;
    }
    //扩容 不可能在原来基础上扩容
    private void grow(){
        this.elem=Arrays.copyOf(this.elem,this.elem.length*2);
    }

}

以上就是关于自己实现的顺序表的增删改查基础操作,在集合框架中的线性表ArrayList,Vector,LinkedList都是实现了List这个接口,我们在下一篇博客中将会介绍一下java.util包下List接口的基本操作~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值