Java顺序表及其基本功能实现

这个博客展示了如何从头实现一个简单的ArrayList类,包括插入、删除、查找、扩容等基本操作。类中使用了一个整型数组存储数据,并提供了打印、添加、判断是否包含元素、查找元素位置、获取和设置元素值等方法。
摘要由CSDN通过智能技术生成
import java.util.Arrays;


public class MyArrayList {
    public int[] elem;
    //数据个数
    public int usedSize;

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

    // 打印顺序表
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        //判断插入位置是否合法
        if(pos < 0 || pos > this.usedSize) {
            System.out.println("pos 位置不和法");
            return;
        }

        //判断数组满不满,满的话扩容
        if (isFull()){
            this.elem = Arrays.copyOf(this.elem,2*usedSize);
        }

        //从后往前移
        for (int i = this.usedSize - 1; i >= pos; i--) {
            this.elem[i + 1] = this.elem[i];
        }
        //在指定位置插入指定元素
        this.elem[pos] = data;
        this.usedSize++;
    }

    //判断满不满
    public boolean isFull() {
        if (this.usedSize == this.elem.length) {
            return true;
        }else {
            return false;
        }
    }
    // 判定是否包含某个元素
    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;
    }
    // 获取 pos 位置的元素
    public int getPos(int pos) {
        if (pos < 0 ||pos >= this.usedSize){
            System.out.println("pos位置不合法");
            return  -1;
        }
        return this.elem[pos];
    }
    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
        if (pos < 0 ||pos > this.usedSize){
            System.out.println("pos 位置不和法");
            return;
        }

        this.elem[pos] = value;
    }

    //判断顺序表是否为空
    public boolean isEmpty(){
        if (this.usedSize == 0){
            return true;
        }
        return false;
    }

    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        if (isEmpty()){
            System.out.println("顺序表为空");
            return;
        }
        for (int i = 0; i < this.usedSize; i++){
            if (this.elem[i] == toRemove) {
                for (;i < this.usedSize - 1; i++){
                    this.elem[i] = this.elem[i + 1];
                }
                usedSize --;
                return;
            }
        }
        System.out.println("要删除的值不存在");
    }
    // 获取顺序表长度
    public int size() {
        return usedSize;
    }
    // 清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值