Data Structure - Dynamic Array CRUD

content

a linear table

2. Dynamic array

3. Add elements

1. Add at the end of the array by default

2. Add index at the specified location

4. Delete

1. Delete the first element

2. Press the button to delete

3. Delete by value

5. Modification

6. Inquiry


a linear table

2. Dynamic array

3. Add elements

1. Add at the end of the array by default

Dynamic array class:

package seqlist;

import java.util.Arrays;

/**
动态数组的类:具备增删改查的功能,逻辑结构对外不可见。用户只知道该功能
 */
public class MyArray {
    //封装的数组
    private int[] data;
    //表示动态数组中元素的个数;
    private int size;

    //如果没有传参,默认开辟大小为是10的数组
    public MyArray(){
        this(10);
    }

    public MyArray(int initCap){
        this.data = new int[initCap];
    }

    public void add(int val){
        data[size] = val;
        size++;
        //元素在添加过程中,元素已经填满数组
        if (size == data.length){
            grow();
        }
    }

    /**
     *
     */
    private void grow(){
        this.data = Arrays.copyOf(data,size*2);
    }

    @Override
    public String toString() {
        return "MyArray{" +
                "data=" + Arrays.toString(data) +
                '}';
    }
}

Test class:

public class Test {
    public static void main(String[] args) {
        MyArray myArray = new MyArray(3);
        myArray.add(10);
        myArray.add(20);
        myArray.add(30);
        myArray.add(40);
        System.out.println(myArray.toString());
    }
}

Test result: The initialized length of the array definition is 3. When the added element is greater than the length of the array, the length of the array is expanded to twice its own.

2. Add index at the specified location

 

public void add(int val,int index){
        //先判断index是否非法,index可以等于size,就相当于在数组的尾部插入,index = 0在头部插入
        if (index < 0 || index > size){
            System.err.println("index is ");
        }
        for (int i = size -1;i >= index ;i--){
            data[i+1] = data[i];
        }
        data[index] = val;
        size++;
        //元素在添加过程中,元素已经填满数组
        if (size == data.length){
            grow();
        }
    }

4. Delete

1. Delete the first element

public int daleteFrist(){
        return delete(0);
    }
    public int deleteLast(){
        return delete(size - 1);
    }

 

2. Press the button to delete

   public int delete(int index){
        if (index < 0 || index > size){
            System.err.println("下标违法");
            return -1;
        }
        int temp = data[index];
        for (int i = index; i < size - 1; i++) {
            data[i] = data[i+1];
        }
        size-- ;
        return temp;
    }

3. Delete by value

public int deleteVal(int val){
        int i = searchByVal(val);
        if (i != -1){
            int delete = delete(i);
            return delete;
        }
        return -1;
    }
    public int[] deleteAllVal(int val){
        MyArray myArray1 = new MyArray(1);
        for (int i = 0; i < size; i++) {
            if (data[i] == val){
                myArray1.add(data[i]);
                delete(i);
            }
        }
        return myArray1.data;
    }

5. Modification

    
    public int updateByIndex(int index,int val){
        if (index < 0 || index > size){
            System.err.println("下标违法");
            return -1;
        }
        int oldVal = data[index];
        data[index] = val;
        return oldVal;
    }
    public boolean updateByVal(int oldVal,int val){
        int i = searchByVal(oldVal);
        if (i != -1){
            data[i] = val;
            return true;
        }
        return false;
    }

6. Inquiry

  public int searchByIndex(int index){
        if (index < 0 || index > data.length - 1){
            System.err.println("下标违法");
            return -1;
        }
        return data[index];
    }
    public int searchByVal(int val){
        for (int i = 0; i < size-1; i++) {
            if (data[i] == val){
                return i;
            }
        }
        return -1;
    }
    public boolean hasContainVal(int val){
        int i = searchByVal(val);
        if (i != -1){
            return true;
        }
        return false;
    }

Guess you like

Origin blog.csdn.net/qq_52655865/article/details/124245481