亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Home Java Javagetting Started Java implements addition, deletion, query and modification of sequence table

Java implements addition, deletion, query and modification of sequence table

Jan 11, 2021 am 09:43 AM
java

Java implements addition, deletion, query and modification of sequence table

What is a sequence table? What kind of structure is it?

The sequence table is a linear structure that is stored sequentially in a segment of storage units with continuous physical addresses. Generally, array storage is used. Complete the addition, deletion, checking and modification of data on the array.

(Learning video sharing: java video tutorial)

The sequence table is divided into:

Static sequence table: Use fixed-length array storage
Dynamic sequence table: Use dynamically opened array storage

The static sequence table is suitable for scenarios where you know how much data needs to be stored.

The fixed-length array of the static sequence table causes N to be fixedly large. If the space is opened too much, it is a waste, and if it is opened too little, it will not be enough.

Sequential table implementation

First of all, you must give the length of a sequence table. If you want to insert elements into the list, we must first insert the first number into position 0. If position 0 Without counting, we cannot directly insert it at position 1 or further back. If there is data at positions 0, 1, and 2, and we want to insert it into position 0 or 1, we have to traverse the sequence table backwards to move the previous data one step back.

Java implements addition, deletion, query and modification of sequence table

Added:

public class SepList {
    public int[] val;//定義數據
    public int size;//存放一個數據則讓size++;

    //構造方法  順序表大小
    public SepList(){
        this.val = new int[5];
    }
    //也可以往進傳大小
    public SepList(int ret){
        this.val = new int[ret];
    }

    //增加數據 得傳要插入的位置與對應位置的數據 就比如0號位置插10
    public void addVal(int pos,int val){
        //首先判斷順序表是否滿
        if(this.val.length == this.size) return;
        //其次得看看給的位置是否合法  pos不能小于0 也不能比如0號位置有數據 1號位置沒有數據  然后插在2號或者更后邊的位置
        if(pos < 0 || pos > this.size) return;
        //如果0 1 2 3位置都有數據,要往1號位置插,得讓后邊的位置往后移一步
        for(int i = this.size; i >= pos; i--){
            this.val[i + 1] = this.val[i];
        }
        //此時在給定位置插數據
        this.val[pos] = val;
        this.size++;
    }

    //打印鏈表
    public void disPlay(){
        for(int i = 0; i < this.size; i++){
            System.out.print(this.val[i] + " ");
        }
        System.out.println();//打印完后空行
    }

    public static void main(String[] args) {
        SepList myList = new SepList();//默認用5個元素
//        SepList myList = new SepList(10);//這時候順序表的大小是10
        myList.addVal(0,10);//在0位置插入第一個數據
        myList.disPlay();//打印

    }
}

//執(zhí)行結果
10

If you want to insert multiple data, just call the method
For example:

        myList.addVal(0,10);//第一次插入
        myList.addVal(1,20);
        myList.addVal(2,30);
        myList.addVal(3,40);
        myList.addVal(4,50);
        myList.disPlay();//打印
 
//執(zhí)行結果
10 20 30 40 50

The order at this time What if the table is full and I insert more?

        myList.addVal(0,10);//第一次插入
        myList.addVal(1,20);
        myList.addVal(2,30);
        myList.addVal(3,40);
        myList.addVal(4,50);
        myList.addVal(5,60);
        myList.addVal(6,70);
        myList.disPlay();//打印

//執(zhí)行結果
10 20 30 40 50

Why is it still the same and no error is reported? This is because when entering the add function, it determines that the sequence table is full. If it is full, it jumps directly to the print function. No error will be reported. At this point the increment function is written.

Check

Java implements addition, deletion, query and modification of sequence table

  //判定鏈表是否包含某個元素
  public boolean contains(int toFind){
      for(int i = 0; i < this.size; i++){
          if(toFind == this.val[i]){
              return true;
          }
      }
      return false;
  }

  //查找某個元素對應的位置
  public int search(int toFind){
      for(int i = 0; i < this.size; i++){
          if(toFind == this.val[i]){
              return i;
          }
      }
      return -1;
  }

  //獲取pos位置的數據
  public int getPos(int pos){
      //首先判斷pos是否合法
      if(pos < 0 || pos > this.size) return -1;
      for(int i = 0; i < this.size; i++){
          if(this.val[i] == this.val[pos]){
              return this.val[pos];
          }
      }
      return -1;
  }

//調用方法 在這沒有粘貼主函數 你們一定要加上
      boolean flag1 = myList.contains(10);//判定元素
      boolean flag2 = myList.contains(60);
      System.out.println(flag1);
      System.out.println(flag2);
      int ret = myList.search(10);//查找
      int ret1 = myList.search(50);
      System.out.println(ret);
      System.out.println(ret1);
      int ret2 = myList.getPos(0);//獲取pos位置數據
      int ret3 = myList.getPos(4);
      System.out.println(ret2);
      System.out.println(ret3);

//執(zhí)行結果
true
false
0
4
10
50

Change

Directly find the data corresponding to the pos position and assign the new data to it Just fine

Java implements addition, deletion, query and modification of sequence table

 //修改pos位置的值
    public void remove(int pos,int val){
        if(pos < 0 || pos > this.size){
            return;
        } else {
            this.val[pos] = val;
        }
    }

        myList.remove(2,3);//2號位置改為3
        myList.remove(3,4);//3號位置改為4
        myList.disPlay();//打印

//執(zhí)行結果
10 20 3 4 50

Delete

After deleting the specified data, just overwrite the subsequent data forward

Java implements addition, deletion, query and modification of sequence table

 //刪除元素
    public void delVal(int key){
        int i,j = 0;
        //找到該位置
        for(i = 0; i < this.size; i++){
            if(this.val[i] == key){
                j = i;
                break;
            }
        }
        //刪除該位置數據,后邊數據往前覆蓋
        for(i = j; i < this.size - 1; i++){
            this.val[i] = this.val[i + 1];
        }
        this.size--;
    }

        myList.delVal(10);
        myList.delVal(50);
        myList.disPlay();//打印

//執(zhí)行結果
20 30 40

Related recommendations: java introductory tutorial

The above is the detailed content of Java implements addition, deletion, query and modification of sequence table. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72
VSCode settings.json location VSCode settings.json location Aug 01, 2025 am 06:12 AM

The settings.json file is located in the user-level or workspace-level path and is used to customize VSCode settings. 1. User-level path: Windows is C:\Users\\AppData\Roaming\Code\User\settings.json, macOS is /Users//Library/ApplicationSupport/Code/User/settings.json, Linux is /home//.config/Code/User/settings.json; 2. Workspace-level path: .vscode/settings in the project root directory

How to handle transactions in Java with JDBC? How to handle transactions in Java with JDBC? Aug 02, 2025 pm 12:29 PM

To correctly handle JDBC transactions, you must first turn off the automatic commit mode, then perform multiple operations, and finally commit or rollback according to the results; 1. Call conn.setAutoCommit(false) to start the transaction; 2. Execute multiple SQL operations, such as INSERT and UPDATE; 3. Call conn.commit() if all operations are successful, and call conn.rollback() if an exception occurs to ensure data consistency; at the same time, try-with-resources should be used to manage resources, properly handle exceptions and close connections to avoid connection leakage; in addition, it is recommended to use connection pools and set save points to achieve partial rollback, and keep transactions as short as possible to improve performance.

Mastering Dependency Injection in Java with Spring and Guice Mastering Dependency Injection in Java with Spring and Guice Aug 01, 2025 am 05:53 AM

DependencyInjection(DI)isadesignpatternwhereobjectsreceivedependenciesexternally,promotingloosecouplingandeasiertestingthroughconstructor,setter,orfieldinjection.2.SpringFrameworkusesannotationslike@Component,@Service,and@AutowiredwithJava-basedconfi

Understanding the Java Virtual Machine (JVM) Internals Understanding the Java Virtual Machine (JVM) Internals Aug 01, 2025 am 06:31 AM

TheJVMenablesJava’s"writeonce,runanywhere"capabilitybyexecutingbytecodethroughfourmaincomponents:1.TheClassLoaderSubsystemloads,links,andinitializes.classfilesusingbootstrap,extension,andapplicationclassloaders,ensuringsecureandlazyclassloa

How to work with Calendar in Java? How to work with Calendar in Java? Aug 02, 2025 am 02:38 AM

Use classes in the java.time package to replace the old Date and Calendar classes; 2. Get the current date and time through LocalDate, LocalDateTime and LocalTime; 3. Create a specific date and time using the of() method; 4. Use the plus/minus method to immutably increase and decrease the time; 5. Use ZonedDateTime and ZoneId to process the time zone; 6. Format and parse date strings through DateTimeFormatter; 7. Use Instant to be compatible with the old date types when necessary; date processing in modern Java should give priority to using java.timeAPI, which provides clear, immutable and linear

Google Chrome cannot open local files Google Chrome cannot open local files Aug 01, 2025 am 05:24 AM

ChromecanopenlocalfileslikeHTMLandPDFsbyusing"Openfile"ordraggingthemintothebrowser;ensuretheaddressstartswithfile:///;2.SecurityrestrictionsblockAJAX,localStorage,andcross-folderaccessonfile://;usealocalserverlikepython-mhttp.server8000tor

Understanding Network Ports and Firewalls Understanding Network Ports and Firewalls Aug 01, 2025 am 06:40 AM

Networkportsandfirewallsworktogethertoenablecommunicationwhileensuringsecurity.1.Networkportsarevirtualendpointsnumbered0–65535,withwell-knownportslike80(HTTP),443(HTTPS),22(SSH),and25(SMTP)identifyingspecificservices.2.PortsoperateoverTCP(reliable,c

Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Comparing Java Frameworks: Spring Boot vs Quarkus vs Micronaut Aug 04, 2025 pm 12:48 PM

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,

See all articles