顺序表的操作实现(学习日记)

17 篇文章 1 订阅

网课网址:https://www.bilibili.com/video/BV1Da4y1s7tb?p=6

 

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @User:Mingaho
 * @Date:2021/04/09
 * @Time:13:32
 */
public class MyArrayList {
    public int[] elem;    //数组
    public int usedSize;   //有效的数据容量
    public static final int capacity = 10;  //初始容量

    public MyArrayList() {
        this.elem = new int[capacity];
        this.usedSize = 0;
    }
    
    private void checkPos(int pos) {
        if(pos < 0 || pos > this.usedSize) {
            throw new RuntimeException("pos' location is not right!");
        }
    }

    private boolean isEmpty() {
        if(this.usedSize != 0) {
            return false;
        }
        return true;
    }

    private boolean isFull() {
        /*if(this.usedSize == this.elem.length) {
            return true;
        }
        return false;*/
        return this.usedSize == this.elem.length;
    }
    
    //打印顺序表
    public void display() {
        for(int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
        //System.out.println(Arrays.toString(this.elem));
    }
    
    //在pos位置新增元素
    public void add(int pos, int data) {
        checkPos(pos);
        if(isFull()) {
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
            //return;
        }
        //挪数据
        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(toFind == this.elem[i]) {
                return i;
            }
        }
        return -1;
    }


    //获取pos下标位置的元素
    public int getPos(int pos) {
        //顺序表是否为空
        if(isEmpty()) {
            //return -1;
            throw new RuntimeException("顺序表为空!"); //手动抛出异常
        }
        //pos是否合法
        if(pos < 0 || pos >= this.usedSize) {
            return -1;
        }
        //checkPos(pos);
        return this.elem[pos];
    }


    //给pos位置的元素设为 value
    public void setPos(int pos, int value) {
        checkPos(pos);
        int ret = search(pos);
        if(ret == -1) {
            return;
        }
        this.elem[ret] = value;
    }


    //删除第一次出现的关键字key
    //是否有该关键字
    //找关键字在哪里 并且知道下标
    public void remove(int toRemove) {
        int index = search(toRemove);
        if(index == -1) {
            System.out.println("number is not need to delete ");
            return;
        }
        for(int i = index; i < this.usedSize - 1; i++) {
            this.elem[i] = this.elem[i + 1];
        }
        this.usedSize--;
       /* int i;
        for(i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toRemove) {
                break;
            }
        }
        for(int j = i; j < this.usedSize - 1; j++) {
            this.elem[j] = this.elem[j + 1];
        }
        this.usedSize--;*/
    }


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


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


    public static void main(String[] args) {
        MyArrayList myArrayList1 = new MyArrayList();
        System.out.println();

    }

}
/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @User:Mingaho
 * @Date:2021/04/09
 * @Time:9:51
 */
public class ShunXuBiao {
    public static void main(String[] args) {
        MyArrayList myArrayList1 = new MyArrayList();
       /* myArrayList1.add(0,12);
        myArrayList1.add(1,18);
        myArrayList1.add(2,3);
        myArrayList1.add(3,21);*/
        for (int i = 0; i < 10; i++) {
            myArrayList1.add(i,i);
        }
        myArrayList1.display();
        myArrayList1.add(10,122);
        myArrayList1.display();

      /*  System.out.println(myArrayList1.getPos(4));
        myArrayList1.display();*/
        myArrayList1.remove(0);
        myArrayList1.display();

      /*  System.out.println("clear");
        myArrayList1.clear();
        myArrayList1.display();*/

        myArrayList1.setPos(8,88);
        myArrayList1.display();


        /*System.out.println("===============");
        System.out.println(myArrayList1.search(51));
        System.out.println(myArrayList1.contains(15));*/

    }
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值