How to use Java to realize the operation of sequential table
Editor to share with you how to use Java to achieve the operation of the sequence table, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The details are as follows
Static sequence table: use fixed-length array storage.
Dynamic sequence table: use dynamically opened array storage.
Interface package com.github.sqlist;public interface ISequence {/ / inserts val boolean add (int pos, Object data) in the pos location; / / finds the keyword key to find the following table that returns key, but does not return-1 int search (Object key); / / finds whether the keyword key is in the sequence table (this conflicts with search) boolean contains (Object key) / / get the value of the pos location Object getPos (int pos); / / delete the keyword key Object remove (Object key) that appears for the first time; / / get the length of the ordered table int size (); / / print the sequential table void display (); / / empty the sequential table to prevent memory leaks void clear ();} implement each method package com.github.sqlist;import java.util.Arrays in the interface / * * sequence table * / public class MySequenceImpl implements ISequence {private Object [] elem; / / number of valid data private int usedSize; private static final int DEFAULT_SIZE = 10; public SqList () {this.elem = new object [default _ SIZE]; this.usedSize = 0 } / * determine whether full * @ return returns true if it is full, otherwise return false * / private boolean isFull () {return this.elem.length = = this.usedSize } / * insert val * @ param pos * the location to be inserted * @ param data * the value to be inserted * @ return * returns true if the insertion succeeds, otherwise false * / @ Override public boolean add (int pos, Object data) {/ / 1. Judge the legitimacy of pos location if (pos)
< 0 || pos >This.elem.length) {return false;} / / 2. Determine whether it is full. If it is full, expand the capacity of if (isFull ()) {this.elem = Arrays.copyOf (this.elem, 2*this.elem.length);} / / 3. Move the pos position and the number after it all back one position for (int I = this.usedSize-1; I > = pos; iMurray -) {this.elem [I] = this.elem [I];} / 4. Insert val this.elem [pos] = data; / / 5 at the pos location. Update length this.usedSize++; return true;} / * determine whether empty * @ return table returns true, otherwise return false * / private boolean isEmpty () {return this.usedSize = = 0 } / * search keyword key finds the following table that returns key, does not return the value of-1 * @ param key keyword * @ return, returns true for successful lookup, and returns false * / @ Override public int search (Object key) {/ / 1 for failure. Determine whether if (isEmpty ()) {return-1;} / / 2 is empty. Traversal search for (int I = 0; I < this.elem.length; iTunes +) {/ / Note: the judgment condition cannot be written as: this.elem [I] = = key if (this.elem.equals (key)) {return I;}} return-1 } / * find whether the keyword key is in the order table (this conflicts with search). The value of the * @ param key keyword * @ return returns true if the lookup succeeds, and false * / @ Override public boolean contains (Object key) {/ / 1 if failed. Determine whether if (isEmpty ()) {return false;} / / 2 is empty. Traversal search for (int I = 0; I < this.elem.length; iTunes +) {/ / Note: the judgment condition cannot be written as: this.elem [I] = = key if (this.elem.equals (key)) {return true;}} return false } / * get the value of pos position * @ param pos the position of the value obtained * @ return successfully get the value of pos position and return true, otherwise return false * / @ Override public Object getPos (int pos) {/ / 1. Determine whether the location is legal if (pos=this.elem.length) {return null;} / / 2. Legal location return this.elem [pos];} / * delete the keyword key * @ param key keyword * @ return * / @ Override public Object remove (Object key) {/ / 1. First check the table to see if there is this keyword / / index: keyword subscript int index = search (key); / / 2. If the keyword if (index = =-1) {return null;} / / 3 is not in the table. In the table, there is the keyword Object data = this.elem [index]; int I; / / delete the keyword key that appears for the first time, and move all the numbers after key forward one position for (I = index; I < this.usedSize; iTunes +) {elem [I] = elem [iTun1];} this.usedSize--; this.elem [iTun1] = null Return data;} / * get the length of the ordered table * @ return the length of the sequential table * / @ Override public int size () {return this.usedSize;} / * print the sequential table * / @ Override public void display () {for (int I = 0; I < this.usedSize) Override public void clear +) {System.out.print (this.eleme [I] + ");} System.out.println ();} / * * clear the sequence table to prevent memory leaks * / @ Override public void clear () {for (int I = 0; I < this.usedSize; item +) {this.elem [I] = null } the correctness of the test method package com.github.sqlist;public class TestDemo1 {public static void main (String [] args) {MySequenceImpl mySequence = new MySequenceImpl (); for (int I = 0; I < 10; iBook +) {mySequence.add (iMagna I);} System.out.println ("insert data in the range of maximum 10:"); mySequence.display () System.out.println (); for (int I = 10; I < 20; iMagazine +) {mySequence.add (iMagazine I);} System.out.println ("expansion:"); mySequence.display (); System.out.println (); System.out.println ("Random position insertion data:"); mySequence.add (9, "list") MySequence.display (); System.out.println (); System.out.println ("search find a data:" + mySequence.search ("list")); System.out.println ("contains find a data:" + mySequence.contains ("list")); System.out.println (); System.out.println ("find the value corresponding to a location:" + mySequence.getPos (9)) System.out.println (); System.out.println ("Delete a data:" + mySequence.remove (8)); mySequence.display (); System.out.println (); System.out.println ("get the length of the ordered table:" + mySequence.size ());}}
Test results:
The above is all the contents of the article "how to use Java to implement the operation of sequence tables". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!