Preface
A linear list is a finite sequence of n data elements with the same characteristics. Linear table is a data structure that is widely used in practice. Common linear tables are: sequence list, linked list, stack, queue, string... Linear table is logically a linear structure, that is to say, it is 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 linked structures.
1. Sequence table
1.1 What is a sequence table
A sequence table uses a storage unit with a continuous physical address to store data in sequence The linear structure of elements is generally stored in an array. Complete the addition, deletion, checking and modification of data on the array.
is actually an array. Then why do you need to write a sequence table? Wouldn't it be better to just use an array? The difference is that writing it in a class can be object-oriented.
Sequence tables can generally be divided into:
Static sequence table: Use fixed-length array storage
Dynamic sequence table: Use dynamically opened array storage
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 too large , it is a waste to open too much space, and it is not enough to open too little.
In contrast, the dynamic sequence table is more flexible, and the space size can be dynamically allocated according to the needs.
2. Simple implementation of the sequence table
2.1 Create sequence table
public class MyArrayList { public int[] elem;//数组 public int usedSize;//数据的有效个数 public MyArrayList(){ this.elem = new int[10]; } }
2.2 Print sequence table
//打印顺序表 public void display(){ for (int i = 0; i < this.usedSize; i++) { System.out.print(this.elem[i] + " "); } System.out.println(); }
2.3 Get sequence table length
//获取顺序表长度 public int size(){ return this.usedSize; }
2.4 in Add a new element at pos position
When inserting an element into the sequence table, the inserted position must be in front of where the element is stored
//在 pos 位置新填元素 public void add(int pos,int data){ if(pos < 0 || pos >usedSize){ System.out.println("pos 位置不合法!"); return; } if(isfull()) { Arrays.copyOf(this.elem,2*this.elem.length); } for (int i = this.usedSize - 1; i >= pos; i--) { this.elem[i + 1] = this.elem[i]; } this.elem[pos] = data; this.usedSize++; } //判断是否满 public boolean isfull(){ return this.usedSize == this.elem.length; }
2.5 Determine whether Contains an element
//判断是否包含某个元素 public boolean contains(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return true; } } return false; }
2.6 Find the position corresponding to an element
//查找某个元素的对应位置,找不到返回-1 public int search(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return i; } } return -1; }
2.7 Get the element at pos position
//获取pos位置的值 public int getPos(int pos){ if(pos < 0 || pos >= 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; }
2.8 Set the element at pos position to value
//给pos位置元素更新value public void setPos(int pos,int value){ if (pos < 0 || pos >= this.usedSize){ System.out.println("pos 位置不合法"); return; } if(isEmpty()){ System.out.println("顺序表为空!"); return; } this.elem[pos] = value; }
2.9 Delete the elements you want to delete
//删除第一次出现的关键字key public void remove(int toRmove){ if (isEmpty()){ System.out.println("顺序表为空!"); return; } int index = search(toRmove); if(index == -1){ System.out.println("没有你要删除的数字!"); return; } for (int i = index; i < this.usedSize - 1; i++) { this.elem[i] = this.elem[i+1]; } this.usedSize--; //this.elem[useSize] = null;如果数组当中是引用数据类型 }
2.10 Clear the sequence list
//清空顺序表 public void clear(){ this.usedSize = 0; }
3. MyArrayList.java
import java.util.Arrays; public class MyArrayList { public int[] elem; public int usedSize; public MyArrayList(){ this.elem = new int[10]; } //打印顺序表 public void display(){ for (int i = 0; i < this.usedSize; i++) { System.out.print(this.elem[i] + " "); } System.out.println(); } //获取顺序表长度 public int size(){ return this.usedSize; } //在 pos 位置新填元素 public void add(int pos,int data){ if(pos < 0 || pos >usedSize){ System.out.println("pos 位置不合法!"); return; } if(isfull()) { Arrays.copyOf(this.elem,2*this.elem.length); } for (int i = this.usedSize - 1; i >= pos; i--) { this.elem[i + 1] = this.elem[i]; } this.elem[pos] = data; this.usedSize++; } //判断是否满 public boolean isfull(){ return this.usedSize == this.elem.length; } //判断是否包含某个元素 public boolean contains(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return true; } } return false; } //查找某个元素的对应位置,找不到返回-1 public int search(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return i; } } return -1; } //获取pos位置的值 public int getPos(int pos){ if(pos < 0 || pos >= 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 public void setPos(int pos,int value){ if (pos < 0 || pos >= this.usedSize){ System.out.println("pos 位置不合法"); return; } if(isEmpty()){ System.out.println("顺序表为空!"); return; } this.elem[pos] = value; } //删除第一次出现的关键字key public void remove(int toRmove){ if (isEmpty()){ System.out.println("顺序表为空!"); return; } int index = search(toRmove); if(index == -1){ System.out.println("没有你要删除的数字!"); return; } for (int i = index; i < this.usedSize - 1; i++) { this.elem[i] = this.elem[i+1]; } this.usedSize--; //this.elem[useSize] = null;如果数组当中是引用数据类型 } //清空顺序表 public void clear(){ this.usedSize = 0; } }
4. Test.java
public class Test { public static void main(String[] args) { MyArrayList myArrayList = new MyArrayList(); myArrayList.add(0,1); myArrayList.add(1,2); myArrayList.add(2,3); myArrayList.add(3,4); myArrayList.add(4,5); myArrayList.display(); System.out.println(myArrayList.contains(3)); System.out.println(myArrayList.getPos(3)); myArrayList.setPos(0,99); myArrayList.display(); } }
The above is the detailed content of How to implement sequential table data structure using Java?. For more information, please follow other related articles on the PHP Chinese website!

Java'splatformindependencemeansdeveloperscanwritecodeonceandrunitonanydevicewithoutrecompiling.ThisisachievedthroughtheJavaVirtualMachine(JVM),whichtranslatesbytecodeintomachine-specificinstructions,allowinguniversalcompatibilityacrossplatforms.Howev

To set up the JVM, you need to follow the following steps: 1) Download and install the JDK, 2) Set environment variables, 3) Verify the installation, 4) Set the IDE, 5) Test the runner program. Setting up a JVM is not just about making it work, it also involves optimizing memory allocation, garbage collection, performance tuning, and error handling to ensure optimal operation.

ToensureJavaplatformindependence,followthesesteps:1)CompileandrunyourapplicationonmultipleplatformsusingdifferentOSandJVMversions.2)UtilizeCI/CDpipelineslikeJenkinsorGitHubActionsforautomatedcross-platformtesting.3)Usecross-platformtestingframeworkss

Javastandsoutinmoderndevelopmentduetoitsrobustfeatureslikelambdaexpressions,streams,andenhancedconcurrencysupport.1)Lambdaexpressionssimplifyfunctionalprogramming,makingcodemoreconciseandreadable.2)Streamsenableefficientdataprocessingwithoperationsli

The core features of Java include platform independence, object-oriented design and a rich standard library. 1) Object-oriented design makes the code more flexible and maintainable through polymorphic features. 2) The garbage collection mechanism liberates the memory management burden of developers, but it needs to be optimized to avoid performance problems. 3) The standard library provides powerful tools from collections to networks, but data structures should be selected carefully to keep the code concise.

Yes,Javacanruneverywhereduetoits"WriteOnce,RunAnywhere"philosophy.1)Javacodeiscompiledintoplatform-independentbytecode.2)TheJavaVirtualMachine(JVM)interpretsorcompilesthisbytecodeintomachine-specificinstructionsatruntime,allowingthesameJava

JDKincludestoolsfordevelopingandcompilingJavacode,whileJVMrunsthecompiledbytecode.1)JDKcontainsJRE,compiler,andutilities.2)JVMmanagesbytecodeexecutionandsupports"writeonce,runanywhere."3)UseJDKfordevelopmentandJREforrunningapplications.

Key features of Java include: 1) object-oriented design, 2) platform independence, 3) garbage collection mechanism, 4) rich libraries and frameworks, 5) concurrency support, 6) exception handling, 7) continuous evolution. These features of Java make it a powerful tool for developing efficient and maintainable software.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Notepad++7.3.1
Easy-to-use and free code editor
