Data structure – Java custom code implementation sequence table, including test cases and the use of ArrayList and related algorithm questions

Welcome to dream_ready’s blog, I believe you are also interested in this blog o (ˉ▽ˉ;)

Introduction to ArrayList and comprehensive step-by-step tutorial on how to use it (with source code), using ArrayList to implement the shuffling algorithm, and 3 people taking turns taking cards (with all source code)

Table of Contents

Introduction to sequence tables

Custom sequence table

SeqList – Constructor

display — print sequence table

fullResize – Determine whether it is full, and expand it if it is full.

add – add a new element, by default at the end of the data

add – add a new element at any position

contains – Determines whether an element is contained

indexOf – Find the position index corresponding to an element

checkPos – Determine whether the parameters are legal

get – get the element at position pos

set – modify/update elements

size – Get the length of the sequence table

remove – delete the first occurrence of the keyword key

clear–Clear the sequence list

All codes of custom sequence table

Custom exception class

Test cases for custom sequence tables

The sequence list that comes with Java – ArrayList

Introduction to sequence table

A sequence table is a linear structure that uses a storage unit with a continuous physical address to store data elements in sequence. Generally, array storage is used. Complete the addition, deletion, checking and modification of data on the array

Customized sequence table

The following are the classes and related methods of the sequence table. Next, I will take you step by step to complete the methods inside and explain the code logic , if you only want to see the complete source code, scroll down to the bottom or click on the complete source code in the directory to go directly to the corresponding location.

First of all, we choose to implement the bottom layer of the sequence table here by an array, and define a variable to store the number of effective elements of the array, and a constant to initialize the memory space of the array.

public class SeqList {
    private int[] array;
    private int size;
    //Default constructor
    SeqList(){ }
    // Determine whether it is full. If it is full, automatically expand the capacity.
    public void fullResize(){ }
    // Add new elements, by default they are added at the end of the array
    public void add(int data) { }
    //Add element at pos position
    public void add(int pos, int data) { }
    // Determine whether an element is contained
    public boolean contains(int toFind) { return true; }
    // Find the position corresponding to an element
    public int indexOf(int toFind) { return -1; }
    // Get the element at pos position
    public int get(int pos) { return -1; }
    // Set the element at pos position to value
    public void set(int pos, int value) { }
    //Delete the first occurrence of the keyword key
    public void remove(int toRemove) { }
    // Get the length of the sequence table
    public int size() { return 0; }
    //Clear the sequence table
    public void clear() { }
    //Print the sequence table. Note: This method is not the method in the sequence table. It is given for the convenience of viewing the test results.
    public void display() { }
}

SeqList–Construction method

Define the construction method and initialize the array length/space. The length is the constant defined above.

 //Construction method, initialize 5 spaces
    public SeqList(){
        this.elem = new int[DEFAULT_CAPACITY];
    }

display–Print sequence table

Traverse the sequence list and print each valid element

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

fullResize – Determine whether it is full, and expand it if it is full

Determine whether it is full. If it is full, automatically expand the capacity.

 // Determine whether it is full. If it is full, automatically expand the capacity.
    public void fullResize(){
        if(this.elem.length == count){
            elem = Arrays.copyOf(elem, 2*elem.length);
        }
    }

add – Add a new element, the default is at the end of the data

New elements are defaulted to the end of the data. When add execution starts, fullResize is first called to determine whether the sequence table is full. If it is full, expand it first and then add elements, because when it is full, there is no position. Add element

 // Add new elements, default at the end of the data
    public void add(int data){
        fullResize();
        elem[count] = data;
        count + + ;
    }

add — Add a new element at any position

When adding an element at pos position, first determine whether pos is legal. For example, if the inserted subscript position is -1 or exceeds the range of the number of valid elements in the array, it is definitely illegal. If it is illegal, an exception will be thrown. Here The exceptions are defined by ourselves. Click Customized Exception Class in the directory to jump, or you can manually browse down.

If legal, move the data and insert it

Why are there two add?

This is an overloading of the method, and the corresponding add method will be automatically selected based on the parameters passed.

 //Add element at pos position
    public void add(int pos, int data){
        fullResize();
        if(pos < 0 || pos > this.count){
            throw new PosOutBoundsException("When adding data, the position is illegal!");
        }
        //Move data
        for(int i = this.count; i > pos; i--){
            this.elem[i] = this.elem[i-1];
        }
        //Save data
        this.elem[pos] = data;
        this.count + + ;
    }

contains – determine whether a certain element is contained

Traverse the sequence list and compare in sequence

 // Determine whether an element is included
    public boolean contains(int toFind){
        for(int i = 0; i <this.count; i + + ){
            if(this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

indexOf – Find the position index corresponding to an element

 // Find the position index corresponding to an element
    public int indexOf(int toFind){
        for (int i = 0; i <this.count; i + + ) {
            if(toFind == this.elem[i]){
                return i;
            }
        }
        return -1; // means there is no such element
    }

The right boundary of the legal range here is calculated based on the number of valid elements.

 // Determine whether the parameters are legal (calculated according to valid elements)
    public boolean checkPos(int pos){
        if(pos < 0 || pos >= this.count){
            return false;
        }
        return true;
    }

get – Get the element at pos position

If pos is illegal, throw an exception

 // Get the element at pos position
    public int get(int pos){
        if(!checkPos(pos)){
            throw new PosOutBoundsException("When getting data, the position is illegal!");
        }
        return this.elem[pos];
    }

set — Modify/update elements

Set the element at pos position to value [Modify/Update]

 // Set the element at pos position to value [modify, update]
    public void set(int pos, int value){
        if(!checkPos(pos)){
            throw new PosOutBoundsException("When setting data, the position is illegal! ");
        }
        this.elem[pos] = value;
    }

size – Get the length of the sequence table

 // Get the length of the sequence table
    public int size(){
        return this.count;
    }

remove – delete the first occurrence of the keyword key

 // Delete the first occurrence of the keyword key
    public void remove(int toRemove){
        //If the list is empty, return directly
        if(this.count == 0){
            return;
        }
        int index = indexOf(toRemove);
        if(index == -1){
            return; //There are no keywords to delete
        }
        for(int i = index; i <this.count-1; i + + ){
            this.elem[i] = this.elem[i + 1];
        }
        this.count--;
    }

clear–Clear the sequence list

 // Clear the sequence list
    public void clear(){
        this.count = 0;
    }

All codes of custom sequence table

The code below is all above, but it is fully displayed below for you to copy and read the code more efficiently.

Friendly reminder: The PosOutBoundsException below is our custom exception. Click Custom exception class in the directory to jump, or you can manually browse down.

import java.util.Arrays;

// sequence table
public class SeqList {
    private int[] elem;
    private int count; // Calculate the number of valid elements currently stored

    private static final int DEFAULT_CAPACITY = 5; //Initialize memory space

    //Construction method, initialize 5 spaces
    public SeqList(){
        this.elem = new int[DEFAULT_CAPACITY];
    }

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

    // Determine whether it is full. If it is full, automatically expand the capacity.
    public void fullResize(){
        if(this.elem.length == count){
            elem = Arrays.copyOf(elem, 2*elem.length);
        }
    }
    // Add new elements, default at the end of the data
    public void add(int data){
        fullResize();
        elem[count] = data;
        count + + ;
    }


    // Determine whether an element is contained
    public boolean contains(int toFind){
        for(int i = 0; i 

Custom exception class

Define a separate .Java file to store the code

It is used to throw an exception when the subscript is illegal. The second construction method means that when an exception is thrown, the information in the message will be printed out.

// Custom exception class
public class PosOutBoundsException extends RuntimeException{
    public PosOutBoundsException(){

    }

    public PosOutBoundsException(String message){
        super(message);
    }
}

Test cases for custom sequence table

This code is used to verify whether the custom sequence table we wrote is correct and functional. From the test, we can see that the code we wrote is very good.

public class Main {
    public static void main(String[] args) {
// SeqList seqList = new SeqList();
// for(int i = 0; i < 10; i + + ){
// seqList.add(i,i*10);
// }
// seqList.display();

        SeqList list = new SeqList();

        //Add elements and print the list
        list.add(1);
        list.add(2);
        list.add(3);
        list.display(); // should print "1 2 3 "

        // Determine whether an element is contained
        System.out.println(list.contains(2)); // should print true
        System.out.println(list.contains(4)); // should print false

        // Find the position index of the element
        System.out.println(list.indexOf(3)); // should print 2
        System.out.println(list.indexOf(4)); // should print -1

        // Get elements
        System.out.println(list.get(0)); // should print 1
        System.out.println(list.get(2)); // should print 3

        // Get the length of the list
        System.out.println(list.size()); // should print 3

        //Set elements
        list.set(1, 5);
        list.display(); // should print "1 5 3 "

        //Insert element at specified position
        list.add(1, 4);
        list.display(); // should print "1 4 5 3 "

        // Delete the first occurrence of the element
        list.remove(4);
        list.display(); // should print "1 5 3 "

        // clear the list
        list.clear();
        list.display(); // Empty lines should be printed
        System.out.println(list.size()); // should print 0
    }
}

The console output is as follows: (completely correct)

The sequence list that comes with Java - ArrayList

Introduction to ArrayList and comprehensive step-by-step tutorial on how to use it (with source code), using ArrayList to implement the shuffling algorithm, and 3 people taking turns taking cards (with all source code)

At this point, thank you for reading this blog and wish you a happy life! o (ˉ▽ˉ;)