In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use Java to achieve a sequence table". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Implement a sequence table
Interface implementation
Define a MyArrayList class and implement the following functions in the class
Public class MyArrayList {}
Definition of array
Public int [] elem;// defines an integer array public int usize;//usize represents the length of the array public MyArrayList () {this.elem = new int [5];}
Print sequence table
For loops to print every bit of the sequence table
Public void display () {for (int I = 0; I)
< this.usize; i++) { System.out.print(this.elem[i]+" "); } System.out.println(); } 在pos位置新增元素 先定义一个isFull函数判断顺序表是否满了,满了返回true,没满则返回false public boolean isFull(){ if (this.usize == this.elem.length){ return true; } return false; } 将pos位置后的元素后移,顺序表顺序表长度增加一位 public void add(int pos, int data){ //判断顺序表是否满了 if (isFull()){ System.out.println("顺序表已满"); //扩容 this.elem = Arrays.copyOf(this.elem,2*this.usize); } //判断pos的合法性 if (pos < 0 || pos >This.usize) {System.out.println ("invalid pos position"); return;} / / move the number after the pos position back to for (int I = this.usize-1; I > = pos; imuri -) {this.elem [pos] = this.elem [I];} this.elem [pos] = data; this.usize++;}
Determine whether an element is included
Public boolean contains (int key) {for (int I = 0; I)
< this.usize; i++) { if (this.elem[i] == key){ return true; } } return false; } 查找某个对应元素的位置 返回它的位置 public int search(int key){ for (int i = 0; i < this.usize; i++) { if (this.elem[i] == key){ return i; } } return -1; } 获取pos位置的元素 定义一个isEmpty函数判断顺序表是否为空 public boolean isEmpty(){ return this.usize == 0; }public int getPos(int pos){ //判断顺序表是否为空 if (isEmpty()){ return -1; } //判断pos 位置是否合法 if (pos < 0 || pos >= this.usize) {return-1;} return this.elem [pos];}
Set the element of the pos location to value and update to the new number
Public void setPos (int pos,int value) {/ / determine whether the sequence table is empty if (isEmpty ()) {return;} / / determine whether the pos location is legal if (pos
< 0 || pos >= this.usize) {return;} this.elem [pos] = value;}
Delete the keyword key that appears for the first time
Find the keyword, move each item forward from the location of the keyword to the end of the order table, overwrite the keyword, and reduce the length by one bit
Public void remove (int key) {int index= search (key); if (key = =-1) {System.out.println ("keyword does not exist"); return;} for (int I = key; I < this.usize-1; iTunes +) {this.elem [I] = this.elem [iTun1];} this.usize--;}
Get the length of the sequence table
Public int size () {return this.usize;}
Clear the sequence table
The length of the sequence table is directly 0.
Public void clear () {this.usize = 0;}
Implement this sequence table
Define a test class to test the output of these functions
Public class TestDemo {public static void main (String [] args) {MyArrayList myArrayList = new MyArrayList (); / / write 1 myArrayList.add (0) to this sequence table; myArrayList.add (1); myArrayList.add (2); myArrayList.add (3); myArrayList.add (4); / / print the sequence table myArrayList.display () / / determine whether the element 5 is System.out.println (myArrayList.contains (5)) in the sequence table; / / find the element 5 that returns its position System.out.println (myArrayList.search (5)); / / get the element System.out.println (myArrayList.getPos (3)) at position 3 / / reassign the element at position 4 to 9 myArrayList.setPos (4); / / print the new sequence table myArrayList.display (); / / delete the element 4 myArrayList.remove (4) that appears for the first time; / / print the new sequence table myArrayList.display () / / get the length of the sequence table System.out.println (myArrayList.size ()); System.out.println ("empty"); / / empty the sequence table myArrayList.clear (); / / print a new sequence table myArrayList.display ();}}
Advantages and disadvantages of sequential tables
Advantages: the sequence table is easy to find, and you can find this element directly if you know the location of this element.
Disadvantages: capacity expansion is generally 2 times the growth, there will be a certain amount of space waste.
This is the end of the content of "how to implement a sequence table with Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.