[Java data structure] manual realization of sequence table

Recently I learned the sequence table in the Java data structure. In order to improve my code ability and understanding of the sequence table, I manually implemented a series of sequence table operations.

The bottom layer of the sequence table is an array. What is more difficult to understand in its series of operations should be adding elements at a certain position and deleting the first occurrence of keywords. This can be better understood by drawing a picture. (Soul drawing on the line) Soul draw on the line
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();
    }
}

operation result

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

Guess you like

Origin blog.csdn.net/qq_45621376/article/details/111888856