One sentence a day
There is only one kind of heroism in the world, that is, to love life after recognizing the truth of life
catalogue
Create classes and member properties and member methods
1. Print sequence table: public void display()
2. Number of returned sequence table data: public int size()
3. Add data at pos: public void add(int pos,int data)
4. Determine whether an element is included: public boolean contains(int toFind)
5. Find the location corresponding to an element: public int search(int toFind)
6. Get the element of pos location: public int getPos(int pos)
7. Set the element of pos position to value: public void setPos(int pos, int value)
8. Delete the first keyword key: public void remove (int to remove)
9. Clear sequence table: public void clear()
1. Data structure
Data structure is a way for computers to store and organize data. A data structure is a collection of data elements that have one or more specific relationships with each other. In general, carefully selected data structures can bring higher operation or storage efficiency.
2. Linearity table
A linear list is a finite sequence of n data elements with the same characteristics.
Linear table is a data structure widely used in practice. Common linear tables: sequential table, linked list, stack, queue, string... Linear table is a linear structure logically, that is, a continuous straight line.
However, the physical structure is not necessarily continuous. When the linear table is stored physically, it is usually stored in the form of array and chain structure.
array
0 | 1 | 2 | 3 | 4 | 5 |
Linear list (linked list)
3. Sequence table
3.1 concept and structure
Sequential table is a linear structure in which data elements are stored in sequence with a storage unit with continuous physical addresses. Generally, array storage is used. Complete the addition, deletion, query and modification of data on the array.
The sequence table can generally be divided into:
- Static sequential table: use fixed length array storage.
- Dynamic sequential table: use dynamic array storage.
The static sequence table is applicable to the scene where you know how much data needs to be stored. The fixed length array of the static sequence table leads to large N, waste of space and insufficient space
In contrast, the dynamic sequential table is more flexible and dynamically allocates space according to needs.
The bottom layer of the sequence table is an array, but we know the size and content of the array.
Create classes and member properties and member methods
public class MyArrayList { public int[] elem; public int usedSize; public MyArrayList(){ this.elem = new int[10]; } }
Array name elem, usedSize indicates the number of valid data, and array initialization this.elem = new int[10];
create object
public class MAIN { public static void main(String[] args) { MyArrayList myArrayList = new MyArrayList(); } }
1. Print sequence table: public void display()
Use the for loop to print
public void display(){ for (int i = 0; i < this.usedSize; i++) { System.out.println(this.elem[i]+" "); } System.out.println(); }
2. Number of returned sequence table data: public int size()
return usedSize directly
public int size(){ return this.usedSize; }
3. Add data at pos: public void add(int pos,int data)
First, judge the legitimacy of pos location:
- If POS < 0, the position is illegal because the subscript of the array starts from 0 and new elements cannot be added at this position;
- POS > usedSize, the position is illegal, because the number of array data is usedSize, and the sequence table can only store data together. Therefore, when POS > usedSize, no new elements can be added at this position;
Secondly, judge whether the sequence table is full:
public boolean isFull(){ return this.usedSize == this.elem.length; }
If the sequence table is full, you need to expand the sequence table. In fact, you can expand the array by Arrays.copyOf(): this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
Note that Arrays.copyOf() returns a new array. Here we need to receive it with the original array.
public void add(int pos,int data){ if(pos < 0||pos > usedSize){ return; } if(isFull()){ this.elem = Arrays.copyOf(this.elem,2*this.elem.length); } } public boolean isFull(){ return this.usedSize == this.elem.length; }
Finally, add data:
To add data at the pos position, you need to move the data behind the pos position (including the pos position) in the sequence table back one bit, then add data at the pos position, and finally usedSize + +.
for (int i = this.usedSize; i >= pos ; i--) { this.elem[i+1] = this.elem[i]; } this.elem[pos] = data; this.usedSize++:
This completes public void add(int pos,int data)
public void add(int pos,int data){ if(pos < 0||pos > usedSize){ return; } if(isFull()){ this.elem = Arrays.copyOf(this.elem,2*this.elem.length); } for (int i = this.usedSize; i >= pos ; i--) { this.elem[i+1] = this.elem[i]; } this.elem[pos] = data; this.usedSize++: } public boolean isFull(){ return this.usedSize == this.elem.length; }
At this time, we simply test the following data addition and sequence table printing
public class MAIN { public static void main(String[] args) { MyArrayList myArrayList = new MyArrayList(); myArrayList.add(0,11); myArrayList.add(1,22); myArrayList.add(2,33); myArrayList.add(3,44); myArrayList.add(4,55); myArrayList.display(); } }
Operation results:
4. Determine whether an element is included: public boolean contains(int toFind)
This can be done using the for loop
public boolean contains(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return true; } } return false; }
5. Find the location corresponding to an element: public int search(int toFind)
If the return subscript is found, but - 1 is not found (because the array subscript has no negative number), it can be completed by using the for loop, The logic is similar to that of public boolean contains(int toFind)
public int search(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return i; } } return -1; }
6. Get the element of pos location: public int getPos(int pos)
1. Judge the legitimacy of pos position:
- If POS < 0, the position is illegal because the subscript of the array starts from 0 and new elements cannot be added at this position;
- POS > usedSize, the position is illegal, because the number of array data is usedSize, and the sequence table can only store data together. Therefore, when POS > usedSize, no new elements can be added at this position
2. Judge whether the sequence table is empty:
public boolean isEmpty(){ return this.usedSize == 0; }
3. Return pos location element
public int getPos(int pos){ if(pos < 0||pos >= this.usedSize){ return -1; } if(isEmpty()){ return -1; } return this.elem[pos]; } public boolean isEmpty(){ return this.usedSize == 0; }
Note: there is no way to return -1 in the first two if statements (because the return value type of public int getPos(int pos) is int, but the elements in the sequence table may also be - 1. It is now written as return -1. Learn more later, and you can throw exceptions here)
7. Set the element of pos position to value: public void setPos(int pos, int value)
1. Judge the legitimacy of pos position:
- If POS < 0, the position is illegal because the subscript of the array starts from 0 and new elements cannot be added at this position;
- POS > usedSize, the position is illegal, because the number of array data is usedSize, and the sequence table can only store data together. Therefore, when POS > usedSize, no new elements can be added at this position
2. Judge whether the sequence table is empty:
Use isEmpty();
3. Change the pos position element to value:
public void setPos(int pos, int value){ if(pos < 0 || pos >=this.usedSize){ return; } if(isEmpty()){ return; } this.elem[pos] = value; }
8. Delete the first keyword key: public void remove (int to remove)
1. Judge whether the sequence table is empty:
Use isEmpty();
2. Find the data to remove and return its subscript:
Here, we use the fifth method search(int toFind) we have written before to complete it
3. After removing toRemove, move the following data forward one bit in turn. this.elem[i] = this.elem[i+1]
4.usedSize--
public void remove(int toRemove){ if(isEmpty()){ return; } int index = search(toRemove) if(-1 == index){ return; } for (int i = index; i < this.usedSize - 1; i++) { this.elem[i] = this.elem[i+1]; } this.usedSize--; }
Note: the boundary of the for loop is this.usedSize - 1, because if i = this.usedSize, i+1 will cross the boundary, resulting in an error
9. Clear sequence table: public void clear()
1. For a sequence table containing only common types (non reference types), set the value of usedSize to 0
public void clear(){ this.usedSize = 0; return; }
2. For the reference type sequence table, you need to set each element in the sequence table to null and usedSize to 0
public void clear(){ for (int i = 0; i < usedSize; i++) { this.elem[i] = null; } this.usedSize = 0; return; }
4. Source code
import java.util.Arrays; public class MyArrayList { public int[] elem; public int usedSize; public MyArrayList(){ this.elem = new int[10]; } //Print sequence table public void display(){ for (int i = 0; i < this.usedSize; i++) { System.out.println(this.elem[i]+" "); } System.out.println(); } //Get sequence table length public int size(){ return this.usedSize; } //Add data at pos public void add(int pos,int data){ if(pos < 0||pos > usedSize){ return; } if(isFull()){ this.elem = Arrays.copyOf(this.elem,2*this.elem.length); } for (int i = this.usedSize; i >= pos ; i--) { this.elem[i+1] = this.elem[i]; } this.elem[pos] = data; this.usedSize++; } public boolean isFull(){ return this.usedSize == this.elem.length; } //Determines whether an element is included public boolean contains(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return true; } } return false; } //Find the corresponding position of an element. If it is not found, return - 1 public int search(int toFind){ for (int i = 0; i < this.usedSize; i++) { if(this.elem[i] == toFind){ return i; } } return -1; } //Gets the element of the pos location public int gotPos(int pos){ if(pos < 0||pos >= this.usedSize){ return -1; } if(isEmpty()){ return -1; } return this.elem[pos]; } public boolean isEmpty(){ return this.usedSize == 0; } //Set the element of pos position to value public void setPos(int pos, int value){ if(pos < 0 || pos >=this.usedSize){ return; } if(isEmpty()){ return; } this.elem[pos] = value; } //Delete the keyword key that appears for the first time public void remove(int toRemove){ if(isEmpty()){ return; } int index = search(toRemove); if(-1 == index){ return; } for (int i = index; i < this.usedSize - 1; i++) { this.elem[i] = this.elem[i+1]; } this.usedSize--; } //Empty sequence table public void clear(){ this.usedSize = 0; return; } }