Array is a commonly used data structure. Array has immutability. The length of the created array is fixed. It accesses elements in the array by index. It has fast access speed and low efficiency of deletion and addition.
Through object-oriented simulation of arrays, the simulated arrays have the following functions:
- Add new elements
- Exhibition
- Find the location of the element
- Get elements by index
- Delete elements by index
- Modify elements at specified locations
Two algorithms are used to operate on the array at the same time:
- Orderly adding elements
- Dichotomous search method
1. Create the array class MyArray.java
How can data be stored? Add a private attribute of array type to the class to store data, and add a variable to store the length of valid data (that is, the number of elements)
When creating an array, you need to specify the length of the array, so you need to add two constructors:
1. Parametric-free constructors set the default length of arrays
2. Parametric constructors specify array length
public class MyArray { //Storage elements private long[] arr; //Represents the length of valid data private int elements; //The default 50 lengths are constructed without parameters public MyArray() { arr=new long[50]; } public MyArray(int maxsize) { arr=new long[maxsize]; } }
2. Writing Method of Adding Data
The default value of the elements attribute is 0. The first element added to the object is also added to the element with index 0. When the length of elements is added by 1, the elements can be added to the array all the time. The number of elements added depends on the length of arr.
public void insert(long value) { arr[elements]=value; elements++; }
3. Writing method of displaying data
Simply show the elements in the array, using for loop traversal
public void display() { System.out.print("["); for (int i = 0; i < elements; i++) { System.out.print(arr[i]+" "); } System.out.println("]"); }
4. Writing methods for finding data
Idea: Using a circular traversal array arr, the data to be searched is compared with each element in the arr. If equal, jump out of the loop. At the end of the loop, if the number of iterations equals the number of elements indicating that no data has been found, return - 1. Otherwise, the number of loops (that is, the index found) is returned.
public int search(long value) { int i ; for (i = 0; i < elements; i++) { if(value==arr[i]) { break; } } //Traveling to the end of the note did not find if (i==elements) { return -1; }else { return i; } }
5. Get elements by index
Idea: This is relatively simple, and elements can be obtained by directly indexing arr. Note, however, that the incoming index must be available, and that unusable indexes can throw exceptions.
public long get(int index) { //If the index is larger than available, or if the index is less than 0, it is invalid. if (index>=elements||index<0) { //Throws an array crossover exception throw new ArrayIndexOutOfBoundsException(); }else { return arr[index]; } }
6. Delete elements by index
Idea: Just like getting elements, check whether the index is available first. If available, traverse backwards from the location of the element to be deleted, assigning the value of the next element to the current element each time. That is to say, the element to be deleted is covered by the next element, the next element is covered by the next element, and so on. When the element is moved, the available element length elements are reduced by 1.
public void delete(int index) { //If the index is larger than available, or if the index is less than 0, it is invalid. if (index>=elements||index<0) { throw new ArrayIndexOutOfBoundsException(); }else { for(int i=index;i<elements;i++) { arr[index]=arr[i+1]; } elements--; } }
7. Modify elements at specified locations
Idea: Just like acquisition, it's about changing acquisition to modification.
public void change(int index,int newValue) { //If the index is larger than available, or if the index is less than 0, it is invalid. if (index>=elements||index<0) { throw new ArrayIndexOutOfBoundsException(); }else { arr[index]=newValue; } }
8. Complete code
public class MyArray { private long[] arr; //Represents the length of valid data private int elements; public MyArray() { arr=new long[50]; } public MyArray(int maxsize) { arr=new long[maxsize]; } /** * Add data * @param value */ public void insert(long value) { arr[elements]=value; elements++; } /** * Display data */ public void display() { System.out.print("["); for (int i = 0; i < elements; i++) { System.out.print(arr[i]+" "); } System.out.println("]"); } /** * Find data */ public int search(long value) { int i ; for (i = 0; i < elements; i++) { if(value==arr[i]) { break; } } //Traveling to the end of the note did not find if (i==elements) { return -1; }else { return i; } } /** * Find the data, according to the index */ public long get(int index) { //If the index is larger than available, or if the index is less than 0, it is invalid. if (index>=elements||index<0) { throw new ArrayIndexOutOfBoundsException(); }else { return arr[index]; } } /** * Delete data */ public void delete(int index) { //If the index is larger than available, or if the index is less than 0, it is invalid. if (index>=elements||index<0) { throw new ArrayIndexOutOfBoundsException(); }else { for(int i=index;i<elements;i++) { arr[index]=arr[i+1]; } elements--; } } /** * Update data */ public void change(int index,int newValue) { //If the index is larger than available, or if the index is less than 0, it is invalid. if (index>=elements||index<0) { throw new ArrayIndexOutOfBoundsException(); }else { arr[index]=newValue; } } }
9. Orderly adding elements
Idea: Modify the insert method and traverse arr arrays. If the current element is larger than the added data, the current location will be saved. Starting with the last element, the elements are moved back one by one, leaving the location to be stored. After saving the element to be added, add the effective data length to 1.
public void insert(long value) { int i; for(i=0;i<elements;i++) { if(arr[i]>value) { break; } } for (int j = elements; j > i; j--) { arr[j]=arr[j-1]; } arr[i]=value; elements++; }
10. Binary Search Method
Thought: Data must be orderly in order to use the dichotomy search method! It can be used in combination with orderly addition elements, where the sequence is ascending (from small to large). Binary lookup is a comparison between the number in the middle of each and a group of numbers. If it is larger, it compares with the number in the middle of the number on the right and the number in the middle of the number on the left if it is smaller. Until the number in the middle is equal to the number to be searched, otherwise there is no such number.
public int binarySearch(long value) { int mid=0;//Intermediate value int low=0; int high = elements; while(true) { mid=(high+low)/2; if(arr[mid]==value) { return mid; }else if(low>high) { return -1; }else { if (value>arr[mid]) { high=mid+1; }else { high=mid-1; } } } }