顺序表入门

public class MyArrayList {
}
实现如下功能:
1.// 打印顺序表
public void display() { }
2.// 在 pos 位置新增元素
顺序表插入元素的时候前驱要有元素!
位置不能满,不能越界
public void add(int pos, int data) { }
3.// 判定是否包含某个元素
public boolean contains(int toFind) { return true; }
4.// 查找某个元素对应的位置
public int search(int toFind) { return -1; }
5.// 获取 pos 位置的元素
public int getPos(int pos) { return -1; }
6.// 给 pos 位置的元素设为 value
public void setPos(int pos, int value) { }
7.//删除第一次出现的关键字key
public void remove(int toRemove) { }
8.//删除所有关键字
public void removeALL(int toRemove) { }
9.// 获取顺序表长度
public int size() { return 0; }
10.// 清空顺序表
public void clear() { }
}
11.//扩容
public void resize(){ }

public class MyArrayList {
    public int[] elem;
    public int usedSize;

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

    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    public void add(int pos, int data) {
        if (pos < 0 || pos > this.usedSize) {
            System.out.println("pos位置不合法");
        } 
        else if (this.usedSize == this.elem.length) {
            System.out.println("顺序表为满");
        } 
        else {
            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] == 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;
    }

    public int getPos(int pos) {
        if (pos < 0 || pos >= this.usedSize) {
            System.out.println("pos位置不合法");
            return -1;
        }
        return this.elem[pos];
    }

    public void setPos(int pos, int value) {
        if (pos < 0 || pos >= this.usedSize) {
            System.out.println("pos位置不合法");
            return;
        }
        this.elem[pos] = value;
    }

  public void remove(int toRemove) { //删除一个数字
        int index = search(toRemove); //找到元素的下标
        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 removeALL(int toRemove){
        for(int i=this.usedSize-1;i>=0;i--){
            remove(toRemove);
        }
        }
        
    public int size() {
        return this.usedSize;
    }
public void clear(){
this.usedSize=0;
}
}

TestDemo

public class TestDemo {
    public static void main(String[] args) {
MyArrayList myArrayList=new MyArrayList(6);
        myArrayList.add(0,5);
        myArrayList.add(0,4);
        myArrayList.add(0,3);
        myArrayList.add(0,2);
        myArrayList.add(0,1);
        myArrayList.add(0,2);
        myArrayList.add(-1,2);
        myArrayList.display();
        System.out.println(myArrayList.contains(1));
        System.out.println(myArrayList.search(2));
        System.out.println(myArrayList.getPos(1));
        System.out.println(myArrayList.size());
        myArrayList.setPos(0,0);
        myArrayList.display();
         myArrayList.remove(2);
        myArrayList.display();
        myArrayList.removeALL(2);
        myArrayList.display();
    }
}

运行结果在这里插入图片描述
remove和removeALL单独运行的
在这里插入图片描述
resize

public class ArrayList {
    public int[] elem;
    public int usedsize;

    public ArrayList(int capacity){
        this.elem=new int[capacity];
        this.usedsize=0;
    }

    public void add(int pos,int data){
        if(pos<0||pos>this.usedsize){
            System.out.println("pos位置不合法");
            return;
        }
        if(this.usedsize==this.elem.length){
            resize();
        }
            for(int i=this.usedsize-1;i>=pos;i--){
                this.elem[i+1]=this.elem[i];
            }
            this.elem[pos]=data;
            usedsize++;
    }

    public void show(){
        for(int i=0;i<this.usedsize;i++){
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }

public void resize(){
this.elem= Arrays.copyOf(this.elem,this.elem.length*2);
return;
}
}
public class TestDemo {
    public static void main(String[] args) {
ArrayList arrayList=new ArrayList(4);
arrayList.add(0,1);
arrayList.add(0,2);
arrayList.add(0,2);
arrayList.add(0,4);
        arrayList.show();
      arrayList.add(2,5);
       arrayList.show();

    }
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值