【Java数据结构】顺序表的手动实现

最近学习了Java数据结构中的顺序表,为了提高下自己的代码能力以及对顺序表的理解,于是手动实现一系列顺序表的的操作。

顺序表的底层就是一个数组,它的一系列操作中比较难理解的应该是在某个位置新增元素以及删除第一次出现的关键字,这个通过画图可以更好地理解。(灵魂画手上线)灵魂画手上线
MyArrayList

import java.util.Arrays;

public class MyArrayList {
    /*public int[] elem = new int[10];
    public int usedSize = 0;
    */
    private int[] elem;//null
    private int usedSize;

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

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

    // 打印顺序表
    public void display() {
        for (int i=0; i<this.usedSize; i++) {
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }
    // 在 pos 位置新增元素
    // 1.full
    // 2.pos是否合法
    // 3.插入数据时,插入位置前一定要有数据
    public boolean isFull(){
        if(this.usedSize==this.elem.length){
            return true;
        }
            return false;
    }
    public void reSize(){
        this.elem= Arrays.copyOf(this.elem,2*this.elem.length);
    }
    public void add(int pos, int data) {
        if(isFull()){
            reSize();
        }
        if(pos<0||pos>this.usedSize){
            System.out.println("pos位置不合法!");
        }
        for(int i=this.usedSize-1;i>=pos;i--){
            this.elem[i+1]=this.elem[i];
        }
        this.elem[pos]=data;
        this.usedSize++;
    }
    //默认插入到数组的最后
    public void add2(int data) {
        //顺序表是否满
        if(isFull()) {
            reSize();
        }
        this.elem[this.usedSize]=data;
        this.usedSize++;
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for(int i=0;i<this.elem.length;i++){
            if(toFind==this.elem[i]){
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int search(int toFind) {
        for(int i=0;i<this.elem.length;i++){
            if(toFind==this.elem[i]){
                return i;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素
    public int getPos(int pos) {
        if(pos<0||pos>=this.usedSize){
            return -1;
        }
        return this.elem[pos];
    }
    // 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
        if(pos<0||pos>=this.usedSize){
            return;
        }
        this.elem[pos]=value;
    }

    // 获取顺序表长度
    public int size() {
        return this.usedSize;
    }
    //删除第一次出现的关键字key
    public void remove(int key) {
        //1.是否有key  index
        //2.i=index   i<useSize-1
        //3.this.useSize--
        int index=search(key);
        if(index == -1) {
            System.out.println("没有啊!老铁");
            return;
        }
        for(int i=index;i<this.usedSize-1;i++){
            this.elem[i]=this.elem[i+1];
        }
        this.usedSize--;
    }
    // 清空顺序表
    public void clear() {
        this.usedSize=0;
    }


}

Test

public class Test {
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add(0,9);
        myArrayList.add(1,19);
        myArrayList.add(2,29);
        myArrayList.display();
        myArrayList.add2(8888);
        myArrayList.add2(11111);
        myArrayList.add2(444444);
        myArrayList.display();
        System.out.println(myArrayList.search(29));
        System.out.println(myArrayList.getPos(5));
        myArrayList.setPos(0,999999);
        myArrayList.display();
        System.out.println(myArrayList.size());
        myArrayList.remove(19);
        myArrayList.display();
        myArrayList.clear();
    }
}

运行结果

9 19 29 
9 19 29 8888 11111 444444 
2
444444
999999 19 29 8888 11111 444444 
6
999999 29 8888 11111 444444 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

_RailGun_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值