Java creates a sequence table (add, delete, modify, check)

The sequence table is a linear structure in which data elements are sequentially stored in a segment of storage units with consecutive physical addresses, generally using array storage. Complete data addition, deletion, search and modification on the array.

The sequence table can generally be divided into:

a. Static sequence table: use fixed-length array storage

b. Dynamic sequence table: use dynamically developed array storage

The static sequence table is suitable for scenarios where you know how much data you need to store

The fixed-length array of the static sequence table causes N to be large, and the space is too wasteful, and the less is not enough. In contrast, the dynamic sequence table is more flexible, and the size of the space is dynamically allocated according to needs.

code show as below:

import java.util.Arrays;

public class MyArray {

    private int[] data;
    private int size;
    // 存储元素仍然还在数组中存储
    public MyArray() {
        data = new int[10];
    }
    // 当前动态数组中实际存储的元素个数
    public MyArray(int dx) {
        data = new int[dx];
    }

    //1.增加元素
    //头插
    public void addFirst(int val){
        addIndex(0,val);
    }
    //尾插
    public void addLast(int val){
        addIndex(size,val);
    }
    //中间插值
    public void addIndex(int index,int val){
        //判断数组是否满
        if(size==data.length){
            grow();
        }
        //判断index是否合法
        if(index<0||index>size){
            System.out.println("add index illegal!");
            return;
        }
        else{
            //将index位置空出
            for (int i = size-1; i >= index; i--) {
                data[i+1]=data[i];
            }
            data[index]=val;
            size++;
        }
    }

    //2.查找元素
    //根据元素查找下标
    public int getValueIndex(int val){
        for (int i = 0; i < size; i++) {
            if(data[i]==val){
                return i;
            }
        }
        return -1;
    }
    //根据下标查找元素
    public int getIndexValue(int index){
        if(index<0||index>size){
            System.out.println("get index illegal!");
        }
        return data[index];
    }

    //3.改变元素
    public void change(int index,int val){
        if(index<0||index>size){
            System.out.println("get index illegal!");
        }
        data[index]=val;
    }

    //4.删除
    public void removeFirst(){
        removeIndex(0);
    }
    public void removeLast(){
        removeIndex(size-1);
    }
    public void removeIndex(int index){
        if(index<0||index>size){
            System.out.println("get index illegal!");
        }
        for (int i = index; i < size-1; i++) {
            data[i]=data[i+1];
        }
        size--;
        data[size]=0;
    }
    public void removeOneValue(int val){
        for (int i = 0; i < size; i++) {
            if(data[i]==val){
                removeIndex(i);
                return;
            }
        }
    }
    public void removeAllValue(int val){
        for (int i = 0; i < size; i++) {
            while(data[i]==val){
                removeIndex(i);
            }
        }
    }

    //5.判断数组中是否有次值
    public boolean isAbove(int val){
        for (int i = 0; i < size; i++) {
            if(data[i]==val){
                return true;
            }
        }
        return false;
    }


    //输出
    public String toString() {
        String ret = "[";
        // 遍历data数组
        for (int i = 0; i < size; i++) {
            ret += data[i];
            if (i != size - 1) {
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }

    //扩容
    private void grow(){
        int[] newData=Arrays.copyOf(this.data,this.data.length<<1);
        this.data=newData;
    }
}

The reference method is as follows:

//顺序表
public class Test {
    public static void main(String[] args) {
        MyArray myArray=new MyArray();

        //增
        System.out.println("增:");
        myArray.addFirst(1);
        myArray.addLast(3);
        myArray.addLast(4);
        myArray.addIndex(1,2);
        System.out.println(myArray);

        //查
        System.out.println("查:");
        System.out.println(myArray.getValueIndex(3));
        System.out.println(myArray.getIndexValue(2));

        //改
        System.out.println("改:");
        myArray.change(1,5);
        System.out.println(myArray);

        //删
        System.out.println("删:");
        myArray.removeIndex(0);
        myArray.removeLast();
        System.out.println(myArray);

        //判断数组中是否有此值
        System.out.println("数组中是否有此值:");
        System.out.println(myArray.isAbove(5));
    }
}

Based on the above input results are as follows:

Guess you like

Origin blog.csdn.net/m0_62218217/article/details/121581579