顺序表各种功能的实现

顺序表各种功能的实现

public class Demo09 {
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add(0,1);
        myArrayList.add(1,2);
        myArrayList.add(1,3);
        myArrayList.add(2,4);
        myArrayList.show();//打印顺序表
        myArrayList.add(3,99);
        myArrayList.show();
        System.out.println(myArrayList.key(99));//判断是否有key元素,true为有
        System.out.println(myArrayList.keyNum(99));//输出key元素的位置
        System.out.println(myArrayList.getPos(3));//输出pos下标的元素
        myArrayList.replace(3,88);//替换pos位置的元素为val
        myArrayList.show();
        myArrayList.del(88);//删除第一次出现的关键字key
        myArrayList.show();
        System.out.println(myArrayList.getLegth());//获取长度
        myArrayList.clear();//清空顺序表
        myArrayList.show();
    }
}

class MyArrayList{
    //普通成员变量
    public int[] elem;
    public int usedSize;
    //构造方法
    public MyArrayList(){
        this.elem = new int[1];
    }

    //判断是否有key元素
    public boolean key (int key){
        for (int i = 0 ; i < this.usedSize; i++ ){
            if(elem[i] == key){
                return true;
            }
        }
        return false;
    }
    //输出key元素的下标
    public int keyNum (int key){
        for (int i = 0 ; i < this.usedSize; i++ ){
            if(elem[i] == key){
                return i;
            }
        }
        return -1;
    }
    //获取pos下标位置的元素
    public int getPos(int pos){
        if (pos<0||pos>=this.usedSize) {
            return -1;
        }
        return this.elem[pos];
    }
    //替换pos位置的元素为val
    public void replace(int pos,int val){
        if (pos<0||pos>=this.usedSize) {
            System.out.printf("pos不在范围内");
        }else {
            this.elem[pos] = val;
        }
    }
    //删除第一次出现的关键字key
    public void del (int key){
        for (int i = 0; i < this.usedSize; i++) {
            if(elem[i] == key){
                for (;i<this.usedSize-1;i++){
                    this.elem[i] = this.elem[i+1];
                }
            }return;
        }
    }
    //获取顺序表的长度
    public int getLegth(){
        return this.usedSize;
    }
    //清空顺序表
    public void clear(){
        this.elem = new int[this.elem.length];
        this.usedSize = 0;
    }
    //给顺序表中添加元素
    public void add(int pos,int data){
        if (this.usedSize == this.elem.length){
            System.out.println("此表已满需要扩容!");
            expand();
        }
        if (pos>this.usedSize || pos<0){
            return;
        }
        for (int i = this.usedSize-1;i >= pos ;i--){
            elem[i+1] = elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }
    //扩容
    public void expand(){
        int[] tmp = new int[this.elem.length*2];
        for (int i = 0 ;i < this.elem.length ; i++ ){
            tmp[i] = this.elem[i];
        }
        this.elem = tmp ;
        return ;
    }
    //打印顺序表
    public void show(){
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值