手写顺序表

该博客展示了如何实现一个自定义的ArrayList类,包括添加、查找、删除、设置元素等操作,并通过TestList进行测试。在添加元素时,会检查数组是否已满并进行扩容;同时,对非法位置的操作会抛出PosWrongfulException。博客还定义了两个自定义异常类:EmptyException和PosWrongfulException。测试中展示了异常处理和不同操作的效果。

demo

MyArrayList

package demo;

import java.util.Arrays;

public class MyArrayList {
    private int[] array;
    private int usedSize;
    public static final int DEFAULT_SIZE = 10;

    public MyArrayList(){
        array = new int[DEFAULT_SIZE];
    }//先开辟一块空间

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

//新增元素,默认在数组最后新增
    /*
    * 1:判断数组是否满了  如果满了就扩容数组
    * 2:在数组后面新增一个元素
    * 3:usedSize++
    * */

    public void add(int data) {
        //1:判断数组是否满了
        if(this.isFull()){
            //如果满了就扩容数组
            array = Arrays.copyOf(array,2*array.length);
        }
        //2:在数组后面新增一个元素
        this.array[usedSize] = data;
        //3:usedSize++
        this.usedSize++;
    }
    public boolean isFull(){
        if(this.usedSize >= this.array.length){
            return true;
        }else{
            return false;
        }
    }

//在 pos 位置新增元素
    /*
    * 1:判断数组是否满了  如果满了就扩容
    * 2:判断pos位置是否合理(下标不可以是负数,不可以隔着放)  如何位置不合理就抛异常
    * 3:将pos后面的元素往后挪动
    * 4:在pos位置添加元素
    * 5:usedSize++
    * */
    public void add(int pos, int data) {
        //1:判断数组是否满了  如果满了就扩容
        if(this.isFull()){
            //如果满了就扩容数组
            array = Arrays.copyOf(array,2*array.length);
        }
        //2:判断pos位置是否合理  如何位置不合理就抛异常
        if(pos < 0 || pos > this.usedSize){
            System.out.println("pos位置不合理");
            throw new PosWrongfulException("pos位置不合理");
        }
        //3:将pos后面的元素往后挪动
        for (int i = this.usedSize; i > pos; i--) {
            this.array[i] = this.array[i-1];
        }
        //4:在pos位置添加元素
        this.array[pos] = data;
        //5:usedSize++
        this.usedSize++;
    }

//判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(array[i] == toFind){
                return true;
            }
        }
        return false;
    }
//查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(this.array[i] == toFind){
                return i;
            }
        }
        return -1;
    }

//获取 pos 位置的元素
    /*
    * 1 判断顺序表是否为空  抛出异常
    * 2 判断pos位置是否合法   不合法就抛出异常
    * 3 获取 pos 位置的元素
    * */

    public int get(int pos) {
        //1 判断顺序表是否为空  抛出异常
        if(this.isEmpty()){
            System.out.println("顺序表为空");
            throw new EmptyException("当前顺序表为空!");
        }
        //2 判断pos位置是否合法   不合法就抛出异常
        if(pos < 0 || pos > this.usedSize){
            System.out.println("pos位置不合理");
            throw new PosWrongfulException("pos位置不合理");
        }
        //3 获取 pos 位置的元素
        return this.array[pos];

    }
    public boolean isEmpty(){
        if(this.usedSize == 0){
            return true;
        }
        return false;
    }

//给 pos 位置的元素设为 value
    /*
    * 1 数组是否为空
    * 2 pos位置是否合法
    * 3 给 pos 位置的元素设为 value
    * */
    public void set(int pos, int value) {
        //1 数组是否为空
        if(this.isEmpty()){
            System.out.println("顺序表为空");
            throw new EmptyException("当前顺序表为空!");
        }
        //2 pos位置是否合法
        if(pos < 0 || pos >= this.usedSize){
            System.out.println("pos位置不合理");
            throw new PosWrongfulException("pos位置不合理");
        }
        //3 给 pos 位置的元素设为 value
        this.array[pos] = value;
    }

//删除第一次出现的关键字key
    /*
    * 1 判断顺序表是否为空
    * 2 删除第一次出现的关键字key
    * 3 usedSize--
    * */
    public void remove(int toRemove) {
        //1 判断顺序表是否为空
        if(this.isEmpty()){
            System.out.println("顺序表为空");
            throw new EmptyException("当前顺序表为空!");
        }
        //2 删除第一次出现的关键字key
        int ret = this.indexOf(toRemove);
        if(ret == -1){
            System.out.println("没有这个数字");
        }else{
            for (int i = ret; i < this.usedSize-1; i++) {
                this.array[i] = this.array[i+1];
            }
            //3 usedSize--
            this.usedSize--;
        }
    }
//获取顺序表长度
    public int size() {
        return this.usedSize;
    }
//清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
}

TestList

package demo;

public class TestList {
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add(1);
        myArrayList.add(2);
        myArrayList.add(3);
        myArrayList.display();
        try {
            myArrayList.add(1,10);
        }catch (PosWrongfulException e) {
            e.printStackTrace();
        }
        myArrayList.display();
        System.out.println("==========================");
        System.out.println(myArrayList.contains(10));
        System.out.println(myArrayList.contains(100));
        System.out.println(myArrayList.indexOf(10));
        System.out.println(myArrayList.indexOf(100));
        System.out.println("==========================");

        try {
            System.out.println(myArrayList.get(1));
        }catch (PosWrongfulException e) {
            e.printStackTrace();
        }

        myArrayList.set(0,99);
        myArrayList.display();
        System.out.println("==========================");

        myArrayList.remove(10);
        myArrayList.display();
        System.out.println("==========================");

        myArrayList.clear();
        myArrayList.display();

        myArrayList.add(19999);
        myArrayList.display();

    }
}

EmptyException

package demo;

public class EmptyException extends RuntimeException{
    public EmptyException(){

    }
    public EmptyException(String message){
        super(message);
    }
}

PosWrongfulException

package demo;

public class PosWrongfulException extends RuntimeException{
    public PosWrongfulException(){

    }
    public PosWrongfulException(String message){
        super(message);
    }
}

结果

1 2 3 
1 10 2 3 
==========================
true
false
1
-1
==========================
10
99 10 2 3 
==========================
99 2 3 
==========================

19999 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

terrychen.hacker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值