顺序表的实现

顺序表的打印、增、删、查、改

import java.util.Arrays;

public class MyArrayList {
    public int [] elem;
    public int usedSize;
    public MyArrayList(){//构造方法给定初始值
        this.elem = new int[5];
    }
    //打印顺序表
    public void disPlay( ){
        for (int i=0;i<usedSize;i++){
           System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }
    //在pos位置新增元素
    public void add(int pos,int data){
        if (pos<0||pos>usedSize){
        System.out.println("pos位置不合法!");
        return;
    }
        //扩容函数
        if(isFull()){
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
            System.out.println("扩容");
        }
        //挪开元素
            for (int i=usedSize-1;i>=pos;i--){
                this.elem[i+1]=this.elem[i];
            }
            this.elem[pos]=data;
            this.usedSize++;
    }
    public boolean isFull(){
        if (this.usedSize==this.elem.length){
            return true;
        }
        return false;
    }
    //判断是否包含某个元素
    public boolean contains(int toFind){
        if (this.isEmpty()){
            System.out.println("顺序表为空!");
            return false;
        }
        for (int i=0;i<this.usedSize;i++){
            if (elem[i]==toFind){
                return true;
            }
        }
        return false;
    }
    //判断数组是否为空
    public boolean isEmpty(){
        if (this.elem==null){
            return true;
        }
        return false;
    }
    //查找某个元素对应的位置/下标
    public int search(int toFind){
        if (isEmpty()){
            return -1;
        }
        for (int i=0;i<usedSize;i++){
            if (this.elem[i]==toFind){
                return i;
            }
        }
        return -1;//找不到返回-1
    }
    //获取pos位置的元素    pos!=usedSize
    public int getPos(int pos){
       
        if (pos<0||pos>=this.usedSize){
            throw new ArrayIndexOutOfBoundsException("pos位置不合法");//自定义一个异常
        }

        return this.elem[pos];
    }
    //获取顺序表有效长度
    public int size() {
        /*if (this.isEmpty()){
            return 0;
        }*/
        return this.usedSize;
    }
    // 给 pos 位置的元素设为 value
    public void setPos(int pos,int value){
        if (isEmpty()){
            return;
        }
        if(pos<0||pos>=this.usedSize){
            throw new ArrayIndexOutOfBoundsException("pos位置不合法");
        }
         this.elem[pos]=value;
    }
    //删除第一次出现的关键字key
    public void remove(int toRemove){
        int index = search(toRemove);
     
        if (index==-1){
            System.out.println("没有你要找的数字"+toRemove);
            return;
        }
        for (int i=index;i<usedSize-1;i++){
            this.elem[i]=this.elem[i+1];
        }
        this.usedSize--;
    }
    //清空顺序表
    public void clear(){
       // this.usedSize = 0;
        for (int i=0;i<usedSize;i++){
            this.elem[i]=0;//若是引用类型=null
        }
        this.usedSize = 0;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值