Use the foundation to make an array that can store data infinitely

HELLO

Today we use the bottom-level array to make a tool that assumes that data can be stored infinitely. First of all, we need to understand why we do this. Everyone knows that the length of an ordinary array is fixed. If your data exceeds its length, then it will There is a case where the subscript is out of bounds, so we need to make a tool that only needs to give the data and we can save it without the problem of no space. Next, we will start to show:

First of all, we create a class in the first step, and then write a method called the storage method. In this method, we need a real array to store our data, and then we need a method to judge whether every Each data has space to store:

 

 Let's extract this judgment method. First, we have to assume a minimum capacity, and then compare it with the capacity of the original array. If the minimum capacity is smaller than the original capacity, we will not change it. If it is greater than the original capacity, then we need Expansion, the following is our judgment method:

    private void ensureCapacityInternal(int minCapacity){
        //判断需要的最小容量,跟现在的element数组的容量比
      if (minCapacity > element.length){
            addsprce(minCapacity);
      }

    }

This addsprce is our expansion method, we will also extract it, specifically written like this, first we need to get the length of the old array, and then use the length of the old array to move right by 1 bit, that is, divide by two and we will expand the capacity , and then get a new array, what if the new space is smaller than the minimum space, we will pay the value of the minimum space together with our new space:

 //经过计算,如果长度不够用创建新数组要多长
    private void addsprce(int minCapacity){
        int oldelement = element.length;

        int newCapacity = oldelement + (oldelement >> 1);

        if (newCapacity < minCapacity){
            newCapacity = minCapacity;
        }

        element= copyOf(element,newCapacity);
    }

After we have this new space, we need to expand the array. We need to replace the data in the original array with our new space. First, we use the new space we calculated to create a new one. Array, and then visit the elements in the old array one by one, and take out the corresponding position in the new array, and then return the new array:

 private int[] copyOf(int[] oldArray,int newCapacity){
        //1.根据计算出来的新的长度,去创建新数组
        int[] newArray =new int[newCapacity];
        //2.将旧数组内的元素挨个访问一遍,取出来放入新数组的对应位置
        for (int i = 0; i < oldArray.length; i++) {
            newArray[i]=oldArray[i];
        }
        return newArray;
    }

Next, we can test. We need to see the length of the array and the actual subscript of the array. Now I give him the default length of 49, and then I store 49 data for testing:

 public static void main(String[] args) {
        TempDemo tempDemo = new TempDemo();
        for (int i = 0; i < 49; i++) {
            tempDemo.add(4);
        }

        System.out.println(tempDemo.size());
        System.out.println(tempDemo.length());
    }

 We can find that the length has not changed, and exactly four or nine data are stored. Next, we store 55 data for testing to see if there will be subscript out of bounds:

    public static void main(String[] args) {
        TempDemo tempDemo = new TempDemo();
        for (int i = 0; i < 55; i++) {
            tempDemo.add(4);
        }

        System.out.println(tempDemo.size());
        System.out.println(tempDemo.length());
    }

We found that he expanded the capacity for us, which means our method is successful!

 

 

The method is very simple, the important thing is how to use and piece together, how to develop your own structural thinking, I hope it can help everyone!

Love life, love code!

Guess you like

Origin blog.csdn.net/m0_67864787/article/details/127978177