Data structure (2) - ArrayList and sequence table

1. Linear table

A linear list is a finite sequence of n data elements with the same properties. Common ones are: sequential list, linked list, stack, queue...

Linear tables must be logically continuous, but physically not necessarily continuous. Physically, they are usually stored in the form of linked lists and arrays.

2. Sequence table

Sequence tables are both logically and physically contiguous.

2.1 Interface implementation

The internal principle and implementation of the interface:

public class MyArraylist {

    public int[] elem;
    public int usedSize;//0
    //默认容量
    private static final int DEFAULT_SIZE = 10;

    public MyArraylist() {
        this.elem = new int[DEFAULT_SIZE];
    }

    /**
     * 打印顺序表:
     * 根据usedSize判断即可
     */
    public void display() {
        if (usedSize == 0) {
            System.out.println("[]");//打印空数组
            return;
        }
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < usedSize; i++) {
            sb.append(elem[i]+" ");
        }
        sb.append("]");
        System.out.println(sb);
    }

    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if (isFull()) {
            grow();
        }
//进行扩容操作
        elem[usedSize] = data;
        usedSize++;
    }

    public void grow() {
        this.elem = Arrays.copyOf(this.elem, this.elem.length * 2);
    }


    /**
     * 判断当前的顺序表是不是满的!
     *
     * @return true:满   false代表空
     */
    public boolean isFull() {
        if (usedSize == elem.length) {
            return true;
        }
        return false;
    }


    private boolean checkPosInAdd(int pos) {
//判断传入位置是否合法
        if (pos < 0 || pos > usedSize) {
            throw new ArrayIndexOutOfBoundsException("数组下标越界");
        }
        return true;
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        checkPosInAdd(pos);
        if (isFull()) {
            grow();
        }
        elem[usedSize] = data;
        usedSize++;
    }


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

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

    // 获取 pos 位置的元素
    public int get(int pos) {
        checkPosInAdd(pos);
        return elem[pos];
    }

    private boolean isEmpty() {
        if (usedSize == 0) {
            return true;
        }
        return false;
    }

    // 给 pos 位置的元素设为【更新为】 value
    public void set(int pos, int value) {
        checkPosInAdd(pos);
        elem[pos] = value;
    }

    /**
     * 删除第一次出现的关键字key
     *
     * @param key
     */
    public void remove(int key) {
        int num = indexOf(key);
        for (int i = num; i < usedSize; i++) {
            elem[num] = elem[num + 1];

        }
        usedSize--;
    }

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

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

2.2 How to use

 boolean add(E e) tail e

void add(int index, E element) insert e into index position

boolean addAll(Collection c) inserts the element E in c

remove(int index) delete the element at index position

boolean remove(Object o) delete the first o E encountered

get(int index) Get the subscript index position element E

set(int index, E element) Set the subscript index position element to element

void clear() Clear boolean contains(Object o) Determine whether o is in the linear table

int indexOf(Object o) returns the index of the first o

int lastIndexOf(Object o) returns the index of the last o

List subList(int fromIndex, int toIndex) intercept part of List

Note: ArrayList expansion is to expand the capacity by twice the current size by default, and use copyOf to expand the capacity

Guess you like

Origin blog.csdn.net/m0_63975371/article/details/127116047