In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how Java can add, delete, modify and query the sequence table of data structure. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Create a sequence table
To implement a sequence table in the Java language, first create a class. Because the sequence table itself is like an array, we define an array of type int and usedata as valid data. In the construction method, we first apply for a space that can store 10 data.
Public class MyArraylist1 {public int [] elem;// number of valid data stored in public int usedata;// Construction method public MyArraylist1 () {this.elem = new int [10];}
Mainly implement the following methods
Public void add (int pos,int data) / / add element public int search (int tofind) / / find an element to return the subscript public void remove (int toRemove) / / delete the keyword toRemove public void setPos (int pos,int value) that appears for the first time / / set the element at pos location to value public void display () / / print element insert element
Before inserting an element, that is, adding an element, we should think about the following steps
Determine whether the sequence table is full
Whether the pos location (the subscript of the inserted element) is legal
How to insert an element
By thinking about it, we know that when if (this.elem.length = = this.usedata), the sequence table is full and can be expanded by the Arrays.copyOf function. If if (posthis.usedata) states that it is illegal, insert the element by this.elem [item1] = this.elem [I], each element moves backward.
/ / add element public void add (int pos,int data) {/ / 1. Determine whether the sequence table is full if (this.elem.length = = this.usedata) {System.out.println ("full, expanded"); this.elem = Arrays.copyOf (this.elem,this.elem.length*2);} / / 2 pos location validity if (posthis.usedata) {System.out.println ("illegal") Return;} / / 3, insert for (int I = this.usedata-1; I > = pos; iMel -) {this.elem [pos] = this.elem [I];} this.elem [pos] = data; usedata++;} find elements
To find the first element that appears, we return its subscript and implement it with search (). Before searching, we first determine whether there is data in the order table. If not, we can manually throw an exception indicating that the sequence table is empty. If we do not find the element we are looking for, we temporarily return a value of-1.
/ / find an element that returns the subscript public int search (int tofind) {/ / 1. Determine whether the sequence table is empty if (this.usedata = = 0) {throw new RuntimeException ("order table is empty");} / / 2. Find for (int I = 0; I < this.usedata; iTunes +) {if (this.usedata = = tofind) {return I;}} return-1; delete the element
Before we delete an element, we need to think about the following questions
How to determine whether a deleted element exists
How to delete
In case the number to be deleted appears more than once
By thinking, we can directly call the method we just wrote to find the element and use index to receive the subscript of the array. If the return value is-1, the element to be deleted does not exist. Otherwise, index will delete the subscript of the element, starting with index, the next element will overwrite this.elem [I] = this.elem [item1], and finally usedata--
Delete the keyword toRemove public void remove (int toRemove) {/ / 1 that appears for the first time. Determine whether the keyword int index = search (toRemove); if (index = =-1) {System.out.println ("deleted keyword does not exist"); return;} / / 2. Delete / / mainly understand that I < this.usedsize-1, drawing is easy to understand, for example, there are 1, 2, 2, 3, 3 numbers, to delete the 0 subscript, iLife0
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.