自定义顺序表ArrayList-------附代码

自定义ArrayList(简单易懂)

1.MyArrayList类

package MyArrayListDemo;

public class MyArrayList {
    //定义数组
    public int[] elem;
    //定义真实大小
    public int usedSize;
    //无参构造,如果不传参,默认初始容量为10
    public MyArrayList(){
        this.elem = new int[10];
        this.usedSize = 0;
    }
    //有参构造,可以传进对应的参数
    public MyArrayList(int capacity){
        this.elem = new int[capacity];
        this.usedSize = 0;
    }

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

    /**
     * 添加元素
     * @param pos
     * @param data
     */
    public void add(int pos, int data){
        //1.判断pos的合法性
        if(pos < 0 || pos > this.usedSize){
            System.out.println("位置不合法!");
            return;
        }
        //2.判断数组是否已满
        if(usedSize == elem.length){
            System.out.println("添加失败!数组已满");
            return;
        }
        //如果是中间位置,那么要把这些元素往后移
        for(int i = usedSize - 1; i >= pos; i--){
            elem[i + 1] = elem[i];
        }

        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();
    }

    /**
     * 判断元素是否存在
     * @param toFind
     * @return
     */
    public boolean contains(int toFind){
        for(int i : this.elem){
            if(i == toFind){
                return true;
            }
        }
        return false;
    }

    /**
     * 查找对应的下标
     * @param toFind
     * @return
     */
    public int search(int toFind){
        for(int i = 0; i < this.elem.length; i++){
            if(toFind == elem[i]){
                return i;
            }
        }
        return -1;
    }

    /**
     * 根据下标获取值
     * @param pos
     * @return
     */
    public int getPos(int pos){
        if(pos < 0 || pos >= this.usedSize){
            return -1;
        }
        return this.elem[pos];
    }

    /**
     * 给下标位置为pos的设置元素值
     * @param pos
     * @param value
     * @return
     */
    public int setPos(int pos, int value){
        if(pos < 0 || pos >= this.usedSize){
            return -1;
        }

        elem[pos] = value;
        return elem[pos];
    }

    /**
     * 根据值删除元素
     * @param toRemove
     */
    public void remove(int toRemove){
        int index = search(toRemove);
        if(index != -1){
            for(int i = index; i < usedSize - 1; i++){
                elem[i] = elem[i + 1];
            }
            this.usedSize--;
        }else{
            System.out.println("您要删除的值不存在!");
        }
    }

    /**
     * 根据下标删除对应的元素
     * @param pos
     */
    public void removeByPos(int pos){
        int value = getPos(pos);
        if(value != -1){
            remove(value);
        }else{
            System.out.println("删除失败!");
        }
    }

    /**
     * 删除所有为该值的元素
     * @param toRemove
     */
    public void removeAll(int toRemove){
        //int step = this.usedSize;
        for(int i = usedSize - 1; i >= 0; i--){
            remove(toRemove);
        }
    }

    /**
     * 清空表
     */
    public void clear(){
        this.usedSize = 0;
    }
}

2.测试类MyArrayListTest

package MyArrayListDemo;

public class MyArrayListTest {
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
//        myArrayList.add(0,1);
//        myArrayList.add(0,2);
//        myArrayList.add(0,3);
        myArrayList.add(0,4);
        myArrayList.add(0,4);
        myArrayList.add(0,4);
        myArrayList.add(0,4);
        myArrayList.add(0,4);
        myArrayList.add(0,4);
        //myArrayList.add(4,5);
        myArrayList.display();
        myArrayList.remove(4);
        myArrayList.display();
        myArrayList.removeByPos(1);
        myArrayList.display();
        myArrayList.removeAll(4);
        myArrayList.display();
        System.out.println(myArrayList.size());

        myArrayList.add(0,1);
        myArrayList.add(0,2);
        myArrayList.add(0,3);
        myArrayList.display();
        myArrayList.clear();
        myArrayList.display();
        System.out.println(myArrayList.size());
    }
}

3.运行结果

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值