In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on the "Java data structure sequence table usage method tutorial", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn the "Java data structure sequence table usage method tutorial"!
Catalogue
1. What is a sequence table?
two。 The basic function and structure of sequential table
3. Realization and Analysis of basic functions of sequence Table
1. Determine whether the linear table is empty or not
two。 Gets the element at the specified location
3. Add elements to a linear table
4. Insert an element at position I
5. Delete the element at the specified location and return the element
6. Find the location where t first appeared
7. Manual expansion method
1. What is a sequence table?
In a program, it is often necessary to manage and use a set of data elements (usually of the same type) as a whole, create such groups of elements, record them with variables, pass them in and out functions, and so on. The number of elements contained in a set of data may change (elements can be added or deleted).
For this requirement, the simplest solution is to regard such a group of elements as a sequence, using the position and order of the elements in the sequence to represent some meaningful information in the practical application, or some relationship between the data.
The organization of such a set of sequence elements can be abstracted as a linear table. A linear table is a collection of elements of a class and records a sequential relationship between elements. Linear table is one of the most basic data structures, which is widely used in practical programs, and it is often used as the basis for the implementation of more complex data structures.
The sequence table is based on the array. We need to implement its specific API function on the basis of the array. What are the following functions?
two。 Basic functions and structure of sequential table public class SequenceList implements Iterable {/ / data private T [] arr; / / record the number of elements in the current sequential table private int N; / / Construction method public SequenceList (int capacity) {this.arr= (T []) new Object [capacity]; this.N=0;}
Parsing: first of all, we need an underlying arr array to store elements, where T refers to generics, because we have not yet determined the type of elements to put, it is possible to put int,String, etc., so we first use generics to express, those who do not understand generics can understand. Secondly, an N is used to count the number of elements in the sequence table. In the constructor, capacity represents the initial length of arr when we create it, because generics cannot be instantiated directly, here we can new an Object array, because Object is the parent of any class, so T is any type we can strongly convert Object to the array we need, N is 0 at the beginning.
Here are the basic functions that the sequence table needs to implement
Public boolean isEmpty () determines whether the linear table is empty public T get (int I) gets the element public void add (T t) of the specified position to add the element tpublic void insert (int iLigue T t) inserts the element tpublic T remove (int I) at the I element deletes the element at the specified position I, and returns the element public int indexOf (T t) to find the position where t first appears public void reSize (int newLength) to manually realize the expansion function 3. The realization and analysis of the basic functions of the sequence table 1. Determine whether a linear table is empty / / set a linear table to an empty table public void clear () {this.N=0;} / / determine whether the current linear table is an empty table public boolean isEmpty () {return empty 0;}
Parsing: to determine whether the linear table is empty, we only need to return whether N is equal to 0.
two。 Get the element at the specified location / / get the element public T get (int I) {return arr [I];} at the specified location
Parsing: the array can directly index the elements in the corresponding position
3. Add an element to a linear table / / add an element t public void add (T t) {if (nude = arr.length) {reSize (2n);} arr [nasty +] = t;}
Parsing: when adding, we first determine whether the array arr is full. If it is full, we will first call our expansion method to increase the array length, which will be parsed later. Then arr [N] can be added to the position, and then N will increase by 1, indicating that the number of elements is one more.
4. Insert element at position I / / insert element t public void insert at I element {if (arr.length = arr.length) {reSize (2cm N);} / move all the elements after the beginning of the I element backward one bit for (int jacks Nashi 1th j > = I Ting JRAT -) {arr [jacks 1] = ARR [j];} Numbai + Arr [I] = t;}
Parsing: to insert elements, we still need to determine whether we need to expand the array, and then we need to move all the elements after the I position back one position through a loop, and finally put t into the arr [I] position, don't forget that N also needs to add 1.
5. Delete the element at the specified location and return it / / delete the element at the specified location I, and return the element public T remove (int I) {if (N)
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.