用java编制的动态顺序表

import java.util.Arrays;

public class MyArrayList {
private int[] elem;
private int usedSize;
private static final int intCapacity = 10;
    public int[] getElem() {
        return elem;
    }
    public void setElem(int[] elem) {
        this.elem = elem;
    }
    public int getUsedSize() {
        return usedSize;
    }
    public void setUsedSize(int usedSize) {
        
        this.usedSize = usedSize;
    }
    public MyArrayList()  {
    this.elem = new int[intCapacity];
    this.usedSize = 0;
    }
    private boolean isFull() { 
        return this.usedSize == elem.length;
    }
//添加数据
    public void add(int pos,int data) {
    if(pos < 0 || pos > this.usedSize) {
        throw new RuntimeException("pos位置不合法");
    }
    if (isFull()) {
        this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
    }
    for (int i = this.usedSize-1; i >= pos; i--) {
        this.elem[i+1] = this.elem[i];
    }
        this.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();
}
//判断是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(toFind == this.elem[i]) {
                return true;
            }
        }
        return false;
}
//查找某个元素对应的位置
    public int search(int toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if (toFind == this.elem[i]) {
                return i;
            }
        }
        throw new RuntimeException("找不到");
    }
    private boolean isEmpty() {
        
        return this.usedSize == 0;
    }
//获取pos位置的元素    
    public int getPos(int pos) {
        if(isEmpty()) {
            throw new RuntimeException("顺序表为空");
        }
        if(pos < 0 || pos>= this.usedSize) {
            throw new RuntimeException("pos位置不合法");
        }
        return this.elem[pos];
}
//删除第一次出现的关键字
    public void remove(int toRemove) {
        if(isEmpty()) {
            throw new RuntimeException("顺序表为空");
        }
        int tmp = search(toRemove);
        for (int i = tmp; i < this.usedSize-1 ; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
}
//清空顺序表
    public void clear() {
        this.usedSize = 0;
    }
    public void setPos(int pos,int value) {
        if(pos < 0 || pos >= this.usedSize) {
            throw new RuntimeException("pos位置不合法");
        }
        this.elem[pos] = value;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值