Get the App
SLTechnology News&Howtos  ›  Development  › 

How to operate the sequence table of Java data structure

Shulou Source: shulou.com Published: 2022-06-01 05:50:37 10月02日 Update

This article mainly introduces the Java data structure of the sequence table how to operate the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Java data structure sequence table how to operate the article will have a harvest, let's take a look.

Preface

A linear table (linear list) is a finite sequence of n data elements with the same characteristics. Linear table is a widely used data structure in practice. Common linear tables are sequential lists, linked lists, stacks, queues, strings. A linear table is logically linear, that is to say, a continuous straight line. However, the physical structure is not necessarily continuous. When linear tables are physically stored, they are usually stored in the form of arrays and chained structures.

I. what is the concept and structure of the sequence table

A sequential table is a linear structure in which data elements are stored successively in a storage unit with a continuous physical address, usually using an array. Complete the addition, deletion, query and modification of data on the array

It's actually an array. Then why write a sequence table and just use an array? Different, write into the class can be object-oriented.

Create a sequence table

The valid number of public class MyArrayList {public int [] elem;// array public int usedSize;// data public MyArrayList () {this.elem = new int [10];}}

Print sequence table

Print the results:

Because display is printed according to usedsize, usedsize is not assigned, so nothing is printed.

Get the length of the sequence table

Add an element to the pos location

The element must be stored in front of the position in which the element is inserted in the sequence table.

Drawing analysis:

The code is as follows:

/ / add an element public void add (int pos, int data) {if (pos) to the pos location

< 0 || pos >

UsedSize) {System.out.println ("pos position is invalid"); return;} if (isFull ()) {this.elem = Arrays.copyOf (this.elem,this.elem.length*2);} for (int I = this.usedSize-1; I > = pos; iMae -) {this.elem [iTh1] = this.elem [I] } this.elem [pos] = data; this.usedSize++;} public boolean isFull () {return this.usedSize = = this.elem.length;}

Print the results:

Decide whether to include an element / / determine whether to include an element public boolean contains (int toFind) {for (int I = 0; I)

< this.usedSize; i++) { if (this.elem[i] == toFind) { return true; } } return false; } 打印结果: 查找某个元素对应的位置 // 查找某个元素对应的位置 public int search(int toFind) { for (int i = 0; i this.usedSize){ System.out.println("pos位置不合法"); return -1;//所以,这里说明一下,业务上的处理,这里不考虑 } if (isEmpty()){ System.out.println("顺序表为空"); return -1; } return this.elem[pos]; } public boolean isEmpty(){//判断为不为空的情况下 return this.usedSize == 0; } 打印结果: 给 pos 位置的元素设为 value // 给 pos 位置的元素设为 value public void setPos(int pos, int value) { if (pos < 0 || pos >

This.usedSize) {System.out.println ("invalid pos location"); return;} if (isEmpty ()) {System.out.println ("sequence table is empty"); return;} this.elem [pos] = value } public boolean isEmpty () {/ / is judged to be not empty, return this.usedSize = = 0;}

Print the results:

Delete the element you want to delete / / delete the keyword keypublic void remove (int toRemove) {if (isEmpty ()) {System.out.println ("order table is empty"); return;} int index = search (toRemove); / / find the location corresponding to the element you want to delete (index = =-1) {System.out.println ("No numbers you want to find"); return } for (int I = index; I

Tags: Order element location structure data linearity result array data structure storage situation physics knowledge content article valid same business number code Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Redmi Microsoft OPPO Reno Shulou Technology