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.
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();//打印 } } //执行结果 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();//打印 //执行结果 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();//打印 //执行结果 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
//判定链表是否包含某个元素 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); //执行结果 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
//修改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();//打印 //执行结果 10 20 3 4 50
Delete
After deleting the specified data, just overwrite the subsequent data forward
//删除元素 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();//打印 //执行结果 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!