Java—implement a sequence table

Java—implement a sequence table

1. The sequence table is a linear structure in which a section of storage units with continuous physical addresses store data elements in sequence. Generally, array storage is used. Complete data addition, deletion, checking and modification on the array.

The sequence table can generally be divided into:
static sequence table: use fixed-length array storage.
Dynamic sequence table: use dynamically developed array storage
.
The static sequence table is suitable for scenarios where you know how much data needs to be stored.
The fixed-length array of the static sequence table causes N to be larger, the space is wasted, and the less is not enough.
In contrast, the dynamic sequence table is more flexible, according to Need to dynamically allocate space size.

2. Implement a sequence table

1. First define the attributes and construction methods of the
sequence table : the bottom layer of the sequence table is an array, so an array and an attribute defining the effective numbers in the array are required, and a construction method is provided. The parameters can be set in the construction method.
Insert picture description here

public class MyArrayList {
    
    
    //顺序表所需要的属性;
    public int[] elem;//引用类型默认为空
    public int usedSize;//不给初始化,默认为0

    //提供构造方法
    public MyArrayList() {
    
    //无参数的构造方法

        this.elem = new int[6];
    }

    public MyArrayList(int capacity) {
    
    //有参数的构造方法
        this.elem = new int[capacity];

    }

2. Realize some operations:
1. Add new elements

// 在 pos 位置新增元素
    public void add(int pos, int data) {
    
    
        if(isFull()){
    
    
            //System.out.println("顺序表为满");
            resize();
            //return;
        }
        if(pos <0 || pos >this.usedSize){
    
    
            System.out.println("pos位置不合法");
        }
        //代码走这说明满足条件,插入
        for (int i =usedSize-1; i >= pos ; i--) {
    
    
            this.elem[i+1] = this.elem[i];//数据从后挪;
        }
        this.elem[pos] = data;
        usedSize++;//重新记录有效数字
    }
    public void add2(int data){
    
    
        if(isFull()){
    
    
            //System.out.println("顺序表为满");
            resize();
            //return;
        }
        this.elem[usedSize]=data;
        usedSize++;//重新记录有效数字
    }

2. Whether it contains an element

// 判定是否包含某个元素
    public boolean contains(int toFind) {
    
    
        for (int i = 0; i <usedSize ; i++) {
    
    
            if(this.elem[i] == toFind ){
    
    
                return true;
            }
        }
        return false;
    }

3. Find the position corresponding to an element

// 查找某个元素对应的位置
    public int search(int toFind) {
    
    
        for (int i = 0; i <usedSize ; i++) {
    
    
            if(this.elem[i] == toFind ){
    
    
                return i;
            }
        }
        return -1;
    }

4. Get the element at pos position

// 获取 pos 位置的元素
    public int getPos(int pos) {
    
    
        if(pos<0 || pos>=usedSize){
    
    
            return -1;
        }
        for (int i = 0; i <usedSize ; i++) {
    
    
            if(i==pos){
    
    
                return this.elem[i];
            }
        }
        return -1;
    }

5. Set the element at position pos as value

// 给 pos 位置的元素设为 value
    public void setPos(int pos, int value) {
    
    
        //判断pos的值是否合法
        if( pos<0 || pos>=usedSize ){
    
    
            return;
        }
        //找到pos;
        for (int i = 0; i <usedSize ; i++) {
    
    
            if(i==pos){
    
    
                this.elem[i]=value;
            }
        }
    }

6. Printing sequence table

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

7. Determine whether the array is full

 public boolean isFull(){
    
    //判断数组是否为满
        if(this.usedSize == elem.length){
    
    
            return true;
        }
        return false;
}

8. Expansion operation

  public void resize(){
    
    //扩容操作
        this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
    }

9. Delete the keyword key that appears for the first time
Insert picture description here

// 删除第一次出现的关键字key
    public void remove(int key) {
    
    
        //1、查找是否有key  index
        int index = search(key);
        if(index == -1) {
    
    
            System.out.println("没有找到");
            return;
        }
        //找到之后i = index;    i < usdSize-1
        for(int i = index;i < this.usedSize-1;i++) {
    
    
            this.elem[i] = this.elem[i+1];
        }
        //3、this.usedSize--;
        this.usedSize--;//长度减小

    }

10. Get the length of the sequence table

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

11. Clear the sequence table

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

Test code:

public class TestDemo {
    
    
    public static void main(String[] args) {
    
    
        MyArrayList myArrayList =new MyArrayList();//有参数
        //myArrayList.add3(123);
        //myArrayList.add3(124);
        //MyArrayList myArrayList1 = new MyArrayList(20);//无参数
        myArrayList.add(0,12);
        myArrayList.add(1,22);
        myArrayList.add(2,32);
        myArrayList.add(3,42);
        myArrayList.add2(123);
        myArrayList.add2(124);
        myArrayList.display();
        System.out.println("===========");

        int ret = myArrayList.usedSize;
        System.out.println(ret);
        System.out.println("========");
        System.out.println(myArrayList.contains(12));
        System.out.println("==========");
        System.out.println(myArrayList.search(12));
        System.out.println("==========");
        myArrayList.remove(32);
        myArrayList.display();
        System.out.println("=========");
        System.out.println(myArrayList.getPos(2));
        System.out.println("========");
        myArrayList.setPos(0,10);
        myArrayList.display();


    }
}

Screenshot of the result:
Insert picture description here
Disadvantages of the sequence table:

  1. Insertion and deletion in the middle/head of the sequence table, the time complexity is O(N)
  2. To increase the capacity, you need to apply for new space, copy data, and release old space. There will be a lot of consumption.
  3. The capacity increase is generally a double increase, and there is bound to be a certain amount of waste of space. For example, the current capacity is 100, and the capacity is increased to 200 when it is full, and we continue to insert 5 data, and there is no data to be inserted later, then 95 data spaces are wasted.

Guess you like

Origin blog.csdn.net/weixin_44436675/article/details/115364909