Java顺序表的基本操作

Java顺序表的操作
1,在pos位置新增元素
2,打印顺序表
3,判断是否包含某个元素
4,查找某个元素的位置
5,获取pos位置的元素
6,获取顺序表长度
7,删除给定的关键词key

import java.util.Arrays;

public class MyArrayList {
    public int[] elem;     //数组
    public int usedSize;   //有效的数据个数
    public static final int intCapacity=10; //初识容量

    public MyArrayList() {
        this.elem=new int[intCapacity];
        this.usedSize=0;
    }

    private boolean isFull() {
        if(this.usedSize==this.elem.length) {
            return true;
        }
        return false;            //这段可以优化为return this.usedSize==this.elem.length;
    }

    private void checkPos(int pos) {
        if(pos<0 || pos>this.usedSize) {
            throw new RuntimeException("pos位置不合法");
        }
    }
    //在pos位置新增元素
    public void add(int pos,int data) {
        checkPos(pos);
        //扩容
        if(isFull()) {
            this.elem= Arrays.copyOf(this.elem,2*this.elem.length);
        }
            for (int i = this.usedSize-1; i >=pos ; i--) {
                this.elem[i+1]=this.elem[i];
            }
            this.elem[pos]=data;
            this.usedSize++;
    }

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

    //判断是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i <this.usedSize ; i++) {
            if(this.elem[i]==toFind) {
                return true;
            }
        }
        return false;
    }

    //查找某个元素的位置
    public int search(int toFind) {
        for (int i = 0; i <this.usedSize ; i++) {
            if(this.elem[i]==toFind) {
                return i;
            }
        }
        return -1;
    }

    private boolean isEmpty() {
        return this.usedSize==0;
    }
    //获取pos位置的元素
    public int getPos(int pos) {
        //是否为空
        if(isEmpty()) {
            throw new RuntimeException("顺序表为空");  //手动抛出异常
        }
        checkPos(pos);
            return this.elem[pos];
    }

    //获取顺序表长度
    public int size() {
        return this.usedSize;
    }

    //删除给定的关键词key
    public void remove(int toRemove) {
        int index=search(toRemove);
        if(index==-1) {
            System.out.println("没有需要删除的数字");
            return;
        }
        for (int i = index; i <usedSize-1 ; i++) {
            this.elem[i]=this.elem[i+1];
        }
        this.usedSize--;
    }

}
public class TestDemo {
    public static void main(String[] args) {
        MyArrayList myArrayList1=new MyArrayList();
        for (int i = 0; i <10 ; i++) {
            myArrayList1.add(i,i);
        }
        myArrayList1.display();
        myArrayList1.add(5,500);
        myArrayList1.display();

        System.out.println(myArrayList1.contains(5));
        System.out.println(myArrayList1.search(5));
        System.out.println(myArrayList1.getPos(3));
        System.out.println(myArrayList1.size());
        myArrayList1.remove(500);
        myArrayList1.display();
    }

}

里插入图片描述
补充:1,判断pos位置是否合法,可以直接写成一个方法,优化代码。
2,判断顺序表是否为空,改为手动抛出异常。
3,注意在新增一个数据时,超出容量要进行扩容。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值