数据结构之顺序表的模拟实现

 "世事犹如书籍,一页页被翻过去。人要向前看,少翻历史旧账。"

作者:Mylvzi 

 文章主要内容:数据结构之顺序表的模拟实现 

 

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 绿字
 * Date: 2023-10-12
 * Time: 8:53
 */
import java.util.*;
/**
 * 顺序表详解
 */
public class MyArrayList {
    private int[] elem;// 存放数据的数组
    private int usedSize;// 有效数据个数

    public static final int DEFAULT_SIZE = 10;

    // 初始化顺序表
    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    public MyArrayList(int ininCapacity) {
        // 自定义数组的大小
        this.elem = new int[ininCapacity];
    }

    // 打印顺序表
    public void display() {
        for (int i = 0; i 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 boolean contains(int toFind) {
        for (int i = 0; i =this.usedSize) {
            throw new posOutOfBoundException(pos+" 位置不合法");
        }


    }

    // 获取 pos 位置的元素
    public int get(int pos) {

        checkPosLegal(pos);
        return this.elem[pos];

    }

    // 给 pos 位置的元素设为 value  pos位置必须含有元素
    public void set(int pos, int value) {
        checkPosLegal(pos);
        this.elem[pos] = value;
    }

    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        int index = indexOf(toRemove);

        for (int i = index; i 
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 绿字
 * Date: 2023-10-12
 * Time: 10:49
 */
public class posOutOfBoundException extends RuntimeException{
    public posOutOfBoundException() {
    }

    public posOutOfBoundException(String message) {
        super(message);
    }
}
/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 绿字
 * Date: 2023-10-12
 * Time: 9:02
 */
public class Test1 {
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList(5);
        myArrayList.add(1);
        myArrayList.add(2);
        myArrayList.add(3);
        myArrayList.add(4);
/*        System.out.println(myArrayList.size());
        myArrayList.remove(1);
        System.out.println(myArrayList.size());*/
//        myArrayList.add(100,99);
/*        myArrayList.set(0,999);
        myArrayList.remove(999);
        myArrayList.remove(2);
        myArrayList.remove(3);
        myArrayList.remove(4);
        myArrayList.remove(5);*/

/*        System.out.println(myArrayList.get(0));
        System.out.println(myArrayList.get(-1));
        System.out.println(myArrayList.get(999));*/
//        System.out.println(myArrayList.contains(2));
//        System.out.println(myArrayList.indexOf(2));
//
/*        myArrayList.add(0,999);
        myArrayList.add(0,999);
        myArrayList.add(0,999);
        myArrayList.add(0,999);
        myArrayList.add(0,999);*/
myArrayList.clear();
        myArrayList.display();
    }
}

你可能感兴趣的:(数据结构,java,开发语言)