Java implements inserting an element at the end of the sequence table

1. Ideas

1. Because we use arrays to implement the sequence table, we must first ensure that the array has enough space to insert elements. 2.
If the array is full, we need to expand the array, and start inserting when it is not full.
3. In the current array The number of elements is the subscript of the end position to be inserted each time.
4. Define a usedSize to represent the current number of elements.
5. When inserting, insert an element into the usedSize subscript.
6. After each successful insertion, Just add one to the number of elements, that is, add one to usedSize.


2. Diagram


The current data is the element to be inserted. The end position refers to the position next to the last element, not the last position of the array. In the current case, the end position is the position of subscript 5.

The above picture shows the situation before inserting a new element. You can see that there are 5 elements in the array at this time, and the value of usedSize is 5. That is to say, the subscript of the position I want to insert is the current usedSize subscript , which is 5 subscripts.



The above is the diagram after insertion, and it can be seen that the data element at this time occupies the position of subscript 5.

If I want to insert a new element at this time, because the number of elements at this time has changed to 6, so although the inserted position is still the usedSize subscript, it has changed from 5 subscripts to 6 subscripts.

The figure below shows the situation of inserting a new element again.


In view of the above, it is enough to prove that the value of usedSize, that is, the current number of elements, is the subscript of the end position to be inserted.


3. How to judge whether the current space of the array is full

Find out the current length of the array and the length of the sequence table (number of elements) , and then compare them. If the number of elements is greater than or equal to the length of the array, the array space is indicated at this time When it is full, it needs to be expanded.

Write a size() method to find the length of the current sequence table.

public int size() {
    
    
    // 直接返回元素个数
    return this.usedSize;
}


Then use the following statement to find the length of the array.

this.elem.length


Comparing the two of them can determine whether the current space is full.

You can write a method to judge whether the space is full or not.

 public Boolean isFull() {
    
    
     // 如果顺序表长度大于或者等于数组的长度就是满了
     //if (size() >= this.elem.length) {
    
    
         // 此时为满了
            //return true;
     //}
     // 此时为不满
     //return false;

     // 简化的写法
     return size() >= this.elem.length; // 根据返回的真假来判断是否满了
 }


If the returned result is true, it means that the space is full and needs to be expanded; if it is false, it means it is not full.


Expansion operation



The above is the situation when the space is full, and the expansion operation will be explained next .

 this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);


Directly use the above statement to expand the array space to 2 times the original size.


Use Arrays.copyOf to expand, elem is the name of the array; 2 means to double the size; elem.length seeks the length of the array.


4. Code

import java.util.Arrays;

class ArrayList {
    
    
    //初始化数组、元素个数
    public int[] elem; // 未初始化的数组
    public int usedSize; // 记录元素个数
    public static final int DEFAULT_SIZE = 10;
    
    // 构造方法
    public ArrayList() {
    
    
        // 初始化数组元素个数为 10 个
        this.elem = new int[DEFAULT_SIZE];
    }

    // 求顺序表的长度
    public int size() {
    
    
        // 直接返回元素个数
        return this.usedSize;
    }

    // 判断是不是空间满了的方法
    public Boolean isFull() {
    
    
        // 如果顺序表长度大于或者等于数组的长度就是满了
        //if (size() >= this.elem.length) {
    
    
            // 此时为满了
            //return true;
        //}
        // 此时为不满
       //return false;

        // 简化的写法
        return size() >= this.elem.length; // 根据返回的真假来判断是否满了
    }

    // 在顺序表末尾位置插入一个元素 data
    public void add(int data) {
    
    
        // 首先要判断空间是不是满了
        if (isFull()) {
    
    
            // 此时满了,要扩容 - 扩容为原来的 2 倍
            this.elem = Arrays.copyOf(this.elem, 2 * this.elem.length);
        }

        // 没满就开始插入操作 usedSize 位置就是末尾的下标
        this.elem[this.usedSize] = data;
        // 元素个数加1
        this.usedSize++;
    }

    // 打印当前顺序表中所有的元素以便于观察结果,这并不是顺序表中的操作!!!
    public void disPlay() {
    
    
        for (int i = 0; i < this.usedSize; i++) {
    
    
            // 输出的元素之间加上空格
            System.out.print(this.elem[i] + " ");
        }
    }
}

public class MyArrayList {
    
    

    public static void main(String[] args) {
    
    
        ArrayList arrayList = new ArrayList();

        // 测试在顺序表末尾插入一个元素的方法
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        // 打印当前的顺序表中的元素
        arrayList.disPlay();
    }
}



According to the output, you can see that the element was successfully inserted.

Guess you like

Origin blog.csdn.net/m0_63033419/article/details/130966318