Java顺序表基本方法实现

本文详细介绍了MyArrayList类,一个基于数组实现的顺序表,包括元素定义、添加、显示、判断元素是否存在、修改和删除等操作,以及处理下标越界异常的方法。
摘要由CSDN通过智能技术生成

定义基本元素

//定义顺序表的基本元素
    public int[] elem;
    //记录当前顺序表当中有多少个有效数据
    public int usedSize;//0

    //默认容量
    private static final int DEFAULT_SIZE = 10;

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

打印顺序表

 public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.print(elem[i]+" ");
        }
        System.out.println();

    }

新增元素

 // 新增元素,默认在数组最后新增
    public void add(int data) {
        //判断是否满了,如果满了就扩容
        if(isFull()){
            resize();//扩容
        }
        elem[usedSize++] = data;

    }

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

判满

/**
     * 判断当前的顺序表是不是满的!
     * @return true:满   false代表空
     */
    public boolean isFull() {
        if(usedSize == elem.length){
            return true;
        }else{
            return false;
        }
    }

判断下标是否合法

 private boolean checkPosInAdd(int pos) {

        return pos < usedSize && pos >= 0;
        //合法
    }

在pos位置新增元素

 // 在 pos 位置新增元素
    public void add(int pos, int data) {
    //先检查pos位置是否合法
        if(!checkPosInAdd(pos)){
        //if(pos < 0 || pos > usedSize){
            throw new ArrayListBoundsOutException("下标位置不合法!");

        }
        //再判断是否满了
        if(isFull()){
            //满了就扩容
            resize();
        }
        //开始移动数据,在pos下标后面的往后移
        for(int i = usedSize - 1; i >= pos ;i--){
            elem[i+1] = elem[i];
        }
        //存放数据
        elem[pos] = data;
        usedSize++;
    }

判断是否包含某个元素

 // 判定是否包含某个元素
    //依次遍历直到找到
    public boolean contains(int toFind) {
    for(int i = 0 ;i < usedSize; i++){
        if(elem[i] == toFind){
            return true;
        }
    }
    return false;
    }

查找元素对应的位置

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

获取pos位置的元素

 // 获取 pos 位置的元素
    public int get(int pos) {
    //先判断pos位置是否合法
    if(!checkPosInAdd(pos)){
        throw new ArrayListBoundsOutException("下标位置不合法!");
    }
    return elem[pos];
    }

判空

private boolean isEmpty() {
        return usedSize == 0;

    }

更新pos位置的值

// 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, int value) {
    if(!checkPosInAdd(pos)){
        throw new ArrayListBoundsOutException("下标位置不合法!");
    }
    elem[pos] = value;
    }

删除第一次出现的关键字key

 public void remove(int key) {
    //如果为空
    if(isEmpty()){
        return;
    }

    //先获取key元素的下标位置,
    int index = indexOf(key);
    //如果没有这个关键字,也需要判断一下
    if(index != -1){
        while(index < usedSize-1){
            elem[index] = elem[index + 1];
            index ++;
        }
    }
    usedSize--;
    }

获取顺序表长度

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

清空顺序表

// 清空顺序表
    public void clear() {
    usedSize = 0;
    }

抛出下标位置不合法异常

重新建一个新类,让它继承RuntimeException,如果下标位置不合法,就会抛出异常

public class ArrayListBoundsOutException extends RuntimeException{
    public ArrayListBoundsOutException(String s) {
        super(s);
    }
}

测试

可以新建一个Test类进行测试,也可以直接在MyArrayList类中添加主方法进行测试,看个人的喜好,一定要测试,有些代码走不通就调试,百分之九十九的代码可以通过调试解决

public class TestMyArrayList {
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add(1);
        myArrayList.add(2);
        myArrayList.add(3);
        myArrayList.add(4);
        myArrayList.display();
        //System.out.println(myArrayList.isFull());
        myArrayList.add(3,8);
        myArrayList.display();
        System.out.println(myArrayList.contains(4));
        //System.out.println(myArrayList.indexOf(9));
        //myArrayList.remove(4);
        //myArrayList.display();
       // System.out.println(myArrayList.size());
        myArrayList.clear();
        myArrayList.display();

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值