Java sequence table [5000 words liver explosion, zero basic nanny set tutorial, you won't hit me]

Posted by kaos057 on Sun, 07 Nov 2021 23:28:27 +0100

Java sequence table [five thousand words explode the liver!! zero basic nanny set tutorial won't you hit me]

preface
At 1 a.m. on November 7, in the LPL global finals, EDG beat DK team to win the championship in Iceland, 7777 kilometers away from China. Let's congratulate EDG

1, Linear table

What is a linear table?

A linear list is a finite sequence of n data elements with the same characteristics. Linear table is a data structure widely used in practice
See the linear table: sequence table, linked list, stack, queue, string

A linear table is logically a linear structure, that is, a continuous straight line.
However, the physical structure is not necessarily continuous, and the linear table exists physically
Storage time is usually stored in the form of array and chain structure.

2, Sequence table

2.1 so what is a sequence table?

Sequential table is a linear structure in which data elements are stored in sequence with a storage unit with continuous physical addresses. Generally, array storage is used. Complete the addition, deletion, query and modification of data on the array.
As the name suggests, the numbers in the sequence table are stored in order, not at random or at intervals


Like this.
Let's think again. Since the sequence table is stored in sequence, whose structure is it similar to?

array

The bottom layer of the sequence table is the array

The sequence table can generally be divided into:

  • Static sequence table
  • Dynamic sequence table

What we should pay attention to is:

Static sequence table is suitable for determining the scene where you know how much data you need to save
The fixed length array of static sequence table leads to large N, waste of space and insufficient space
In contrast, dynamic sequential tables are more flexible and dynamically allocate space as needed

Then the question comes again: since the bottom of the sequence table is array, why not use array directly?? why??

Let's draw a picture:
How many valid data are there in this array??

You may say: jump out when it is equal to 0, count!!!
This is not feasible:

What if 0 is also data? Right, that'll be a problem.

Then we can define a variable: this variable is called useSide (representing how much valid data is currently in it)

Here are four data:

So we will realize:
A sequence table can be implemented with more than one array, and other variables (such as (usedSize) are required

Note: at the same time, I want to tell you: don't count yourself. Let the program do it!!
Learn data structure, never recite code, be sure to understand!!!

code implementation

First, we find that this object is a sequence table, which contains arrays, valid array variables and an array capacity:

2.2 implementation sequence table

According to the above understanding, we know the three factors needed to implement the sequence table:

  1. array
  2. Valid array
  3. Available capacity
public class MyArrayList {
    public int[] elem;  //array
    public int useSize; //Valid array
    public static int capacity = 10; //Available capacity

    public MyArrayList() {   //Initial capacity using construction method
        this.elem = new int[capacity];
    }
}

You can read another blog I wrote about constructors:

3, Interface (common operation) implementation of sequence table:

// Print sequence table
public void display() { }
// Add new element in pos position
public void add(int pos, int data) { }
// Determines whether an element is included
public boolean contains(int toFind) { return true; }
// Find the location of an element
public int search(int toFind) { return -1; }
// Gets the element of the pos location
public int getPos(int pos) { return -1; }
// Set the element of pos position to value
public void setPos(int pos, int value) { }
//Delete the keyword key that appears for the first time
public void remove(int toRemove) { }
// Get sequence table length
public int size() { return 0; }
// Empty sequence table
public void clear() { }

Does your scalp feel numb?

Don't panic, let's watch one by one

1. Print sequence table

Idea: just traverse the array

Note that the length of the traversal is useSize (print valid numbers)

// Print sequence table
        public void display() {
            for (int i = 0; i < this.useSize; i++) {
                System.out.print(elem[i] + " ");
            }
            System.out.println();
        }

2. Add a specified element in the pos position

Put the specified data in the specified location:

public void add(int pos,int data){
	
}

We need to execute the add function in the sequence table to perform the function of adding data in the pos position.

Next, we need to talk about the details:

  • Judge whether the array is full. If it is full, expand the capacity
  • Judge whether the stored location subscript is legal (whether the array is out of bounds)
  • If you want to insert data into an array at the pos position, you must first put the following array one step behind, so that there will be a position in front
  • Store the data and remember useSize + +; (add 1 to the valid data)

Let's take a look at the picture analysis:

1, 2, 3, 4 and 5 are stored in sequence. We need to insert elements at position 3, so we just need to move 3, 4 and 5 back one position.

Let's take a look at the code:

		public boolean isFull() {
        /*if(this.usedSize == capacity) {
            return true;
        }
        return false;*////Two methods
        return this.usedSize == capacity;//Judge whether the useSize is the same as the maximum capacity. If it is the same, it will be full
    }
		public void add(int pos, int data) {
		//1. Legal status of pos
        if(pos < 0 || pos > this.usedSize) {
            System.out.println("pos Illegal location!");
            return;
        }
        //2. Full condition 
        if(isFull()) {
            //Capacity expansion
            this.elem = Arrays.copyOf(this.elem,2*capacity);
            capacity *= 2;//New capacity
        }
        for(int i = this.usedSize-1; i >= pos;i--) {
            this.elem[i+1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

3. Determine whether an element is included

Idea:

  1. The traversal length is: useSize
  2. Traverse the array to see which element is equal to toFind (the element to be judged)
//Judge whether the array is empty (whether the valid data is 0)
public boolean isEmpty() {
        return this.usedSize == 0;
    }
public boolean contains(int toFind) {
        if(isEmpty()) return false;
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

4. Find the corresponding position of an element

Idea:

  1. Traverse the array to find the return subscript
  2. Note that the traversal length of the array is useSize
	public boolean isEmpty() {
        return this.usedSize == 0;
    }
	public int search(int toFind) {
        if(isEmpty()) return -1;
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

5. Get the element of pos position

Idea:
Returns an array of pos subscripts directly

public int getPos(int pos) {
        if(isEmpty()) {
            //return -1; business processing
            throw new RuntimeException("The sequence table is empty");//Manually throw an error (exception)
        }
        if(pos < 0 || pos >= this.usedSize) {
            throw new RuntimeException("pos wrongful");//Manually throw an error (exception)
        }

        return this.elem[pos];
    }

6. Get sequence table length

Idea: just return to useSize directly

public int size() {
        return this.usedSize;
    }

7. Set the element of pos position to value

Idea: just assign the value directly, and pay attention to the legitimacy of the subscript

public void setPos(int pos, int value) {
        if(pos < 0 || pos >= this.usedSize) {
            System.out.println("pos wrongful!");
            return;
        }
        if(isEmpty()) {
            System.out.println("Sequence table is empty!");
            return;
        }
        this.elem[pos] = value;
    }

8. Delete the keyword key that appears for the first time

The code is very similar to add. Find the element to be deleted, and then move the i+1 element to the current i element. Finally, useSize – 1

Picture analysis:

public void remove(int toRemove) {
        if(isEmpty()) return;
        int index = search(toRemove);
        if(index == -1) {
            System.out.println("There are no numbers you want to delete");
            return;
        }
        for (int i = index; i < this.usedSize-1; i++) {
            this.elem[i] = this.elem[i+1];
        }
        this.usedSize--;
    }

9. Empty the sequence table

Idea: let each element of the array be 0 (the valid data is also 0)

public void clear() {
        for (int i = 0; i < this.usedSize; i++) {
            this.elem[i] = 0;
        }
        this.usedSize = 0;
    }

I hope my explanation can help you, byebye.

Topics: Java data structure