简单实现Arraylist链表底层代码

import java.util.Arrays;

public class MyArraylist {

        public int[] elem;
        public int usedSize;//0
        //默认容量
        private static final int DEFAULT_SIZE = 10;

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

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

        // 新增元素,默认在数组最后新增
        public void add(int data) {
                if (isFull()) {
                        System.out.println("原数组已经满了");
                        this.elem =
                                Arrays.copyOf(this.elem, 2 * this.elem.length);
                }
                this.elem[this.usedSize] = data;
                this.usedSize++;
        }

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


        private boolean checkPosInAdd(int pos) {
                if (pos >= this.elem.length || pos < 0) {
                        System.out.println("所输入的pos不合法");
                        return false;
                }
                return true;//合法
        }

        // 在 pos 位置新增元素
        public void add(int pos, int data) throws PosWrongfulException {
                if (isFull()) {
                        System.out.println("原数组已满");
                        this.elem =
                                Arrays.copyOf(this.elem, 2 * this.elem.length);
                }
                if (!checkPosInAdd(pos)) {
                        System.out.println("所输入的pos不合法");
                        throw new PosWrongfulException("pos位置不合法");
                }
                for (int i = this.usedSize - 1; i >= pos; i--) {
                        this.elem[i + 1] = this.elem[i];
                }
                this.elem[pos] = data;
                this.usedSize++;

        }

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

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

                // 获取 pos 位置的元素
                public int get_pos ( int pos){
                if(isEmpty()){
                        System.out.println("数组为空无法获取");
                        throw new EmptyException("当前顺序表为空!");
                }
                if(!checkPosInAdd(pos)){
                        System.out.println("所输入的pos不合法");
                        throw new PosWrongfulException("pos位置不合法");
                }
                return this.elem[pos];
                }

                private boolean isEmpty () {
                        return this.usedSize == 0;
                }
                // 给 pos 位置的元素设为【更新为】 value
                public void set_pos ( int pos, int value){
                        if(isEmpty()){
                                System.out.println("数组为空无法获取");
                                throw new EmptyException("当前顺序表为空!");
                        }

                        if(!checkPosInAdd(pos)){
                                System.out.println("所输入的pos不合法");
                                throw new PosWrongfulException("pos位置不合法");
                        }

                        this.elem[pos]=value;

                }

                /**
                 * 删除第一次出现的关键字key
                 * @param key
                 */
                public void remove ( int key){
                        if(isEmpty()){
                                System.out.println("数组为空无法获取");
                                throw new EmptyException("当前顺序表为空!");
                        }
                        if(!checkPosInAdd(key)){
                                System.out.println("所输入的key不合法");
                                throw new PosWrongfulException("key位置不合法");
                        }
                        int index=indexOf(key);
                        if(index==-1){
                                System.out.println("不存在此数字");
                        }
                        for(int i=index;i<this.usedSize;i++){
                                this.elem[i]=this.elem[i+1];
                        }
                }

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

                // 清空顺序表
                public void clear () {
            for(int i=0;i<this.usedSize;i++){
                    this.elem[i]=0;
                    this.usedSize--;
            }
                }

        }
----------------------分割线-------------------------------
public class PosWrongfulException extends RuntimeException{
    public PosWrongfulException() {

    }
    public PosWrongfulException(String message) {
        super(message);
    }
}
---------------------分割线---------------------------------
public class EmptyException extends RuntimeException{
    public EmptyException() {
    }

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

### STM32端口配置中的高低寄存器 #### GPIOx_CRL (端口配置低寄存器) 对于STM32微控制器而言,端口配置低寄存器(GPIOx_CRL)负责管理引脚0至7的功能设置[^1]。此寄存器允许开发者定义较低编号引脚的操作模式及其具体行为特性。每个引脚由四位控制段表示,意味着单个寄存器能够容纳八个这样的四比特组来分别指定各引脚的工作方式。 ```c // 设置GPIOA的前8个引脚为推挽输出模式,最大速度50MHz RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | ... | GPIO_Pin_7; // 选择要初始化的针脚 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出模式 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 最大频率50Mhz GPIO_Init(GPIOA, &GPIO_InitStructure); // 应用上述设定到GPIOA上 ``` #### GPIOx_CRH (端口配置高寄存器) 另一方面,端口配置高寄存器(GPIOx_CRH)则专注于处理同一端口中较高编号的部分—即从第8号到第15号之间的引脚[^3]。同样地,它也通过每四个连续位代表单一引脚的方式来进行详细的属性定制化操作。这种设计使得用户可以在不改变其他部分的情况下单独调整特定范围内的I/O接口性能参数。 ```c // 类似于上面的例子,这里是对GPIOA后面的8个引脚进行相同类型的配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | ... | GPIO_Pin_15; GPIO_Init(GPIOA, &GPIO📐⚗📐 📐⚗📐📐📐⚗📐📐📐⚗📐📐📐⚗📐📐📐⚗📐📐📐⚗📐📐
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值