# Data Structure and Array Implementation of Algorithmic Series

## Data Structure and Algorithmic Series (1) Array Implementation

Note: This is a new series, mainly because data structure and algorithms are the basis of programmers'future lives. I have studied them in University before, but I soon forgot. Now I want to pick it up, write a series of articles to improve my understanding. In fact, I write this series mainly by preheating, then brushing the leetcode.The brushing algorithm itself is to exercise the thinking of writing programs by itself. It does not want to stiffen your thinking by writing business code every day. This series will be updated with the springboot series and set up a falg.

## java implementation array

Explain:

• An array is a continuous structure that stores the same type of data and is a linear structure, because it is a linear structure, so every data in the array has a front and back, but note that the start (first) and end data are not included.
• An important feature of arrays is random access

### 1. Define arrays

#### Arrays have two basic variables:

• Array length: means the size of the array itself
• Continuous space in which arrays store specific data
`````` // Length of Array Request Space
private int size = 0;
// The actual length of the array
private int count;
// Array Actual Storage
private int array[];
``````

### 2. Basic methods: adding, deleting, deleting and altering construction methods

#### Construction method

``````   /**
* Construction method - Initialization
* @param capacity Array Initialization Length
*/
public MyArray(int capacity) {
this.size = capacity;
this.array = new int[capacity];
this.count = 0;
}
``````
``````  // Initialize space size using construction methods
MyArray myArray = new MyArray(6);
``````

##### Note: What's new is essentially inserting new data in contiguous space, which I currently know are two
• The first is to move the data one bit backwards starting at the end of the array, freeing up the position that the index points to for easy insertion of new data.As shown in the figure, move 5544 33 in turn, so that the position is empty. Never move 33 first. If you move 33 first, 33 will directly cover 44.The code is as follows

• The second is to take the data directly from the specified index position and place it at the end of the array, which avoids the overall movement of the array, which can be considered when there is a large amount of data.
``````   /**
* Insert data at specified location based on index
* @param index Indexes
* @param value Value with insertion
*/
protected boolean myArrayInsert(int index,int value){

// Determine if there is still free space in the array
if (count == size){
System.out.println("No space to insert");
return false;
}

// Determine whether or not you are out of bounds
if (index < 0 || index >= size){
System.out.println("Array out of bounds exception");
return false;
}

// Loop, moving the data backwards in turn from the insertion position, freeing up the position that the index points to for easy insertion of new data
for (int i = count; i > index; i--) {
array[i] = array[i-1];
}
array[index] = value;
count ++ ;
System.out.println("Insert Successful");
return true;
}
``````

#### Delete: As new, there are still two ways

• First, the index index begins at the location where it was deleted, and the elements that follow move forward one by one, overwriting the previous one.But you still need to move every element after the index.

• Second, the simplest is to place the last element of the array directly in the index position, which reduces data movement
``````   /**
* Delete the number of specified locations
* @param index Indexes
*/
protected boolean myArrayDel(int index){
if (index < 0 || index >= count){
System.out.println("Index out of bounds");
return false;
}

for (int i = index; i < count - 1; i++) {
array[i] = array[i + 1];
}
count --;
System.out.println("Delete succeeded");
return true;
}
``````

#### Query: Returns the index value of the data after the query succeeds

``````   /**
* Array Query
* @param value Values to Query
* @return Returns the index corresponding to this value
*/
protected int myArrayFind(int value){
for (int i = 0; i < count; i++) {
if (array[i] == value){
System.out.println("query was successful");
return i;
}
}
System.out.println("Query unsuccessful, the number does not exist");
return -1;
}
``````

#### modify

``````   /**
* Modify and replace data at specified locations
* @param index Specify Location Index
* @param value value
* @return Is the modification successful
*/
protected boolean myArrayModify(int index,int value){
if (index < 0 || index >= count){
System.out.println("Index out of bounds");
return false;
}

array[index] = value;
return true;
}
``````

#### Print Output: Provides printing methods for query purposes

``````   /**
* Array Printing
*
*/
protected void printAll(){
System.out.println("Current Array Actual Length:" + count);
System.out.println("The size of the array space requested:" + size);
for (int i = 0; i < count; i++) {
System.out.println("position:" + i + "----" + array[i]);
}
}
``````

#### test

``````public static void main(String[] args) {
MyArray myArray = new MyArray(6);
myArray.myArrayInsert(0,0);
myArray.myArrayInsert(1,1);
myArray.myArrayInsert(2,2);
myArray.myArrayInsert(3,3);
myArray.myArrayInsert(4,4);
myArray.myArrayInsert(5,5);

myArray.myArrayInsert(2,3);
// delete
myArray.myArrayDel(0);

// query
int i = myArray.myArrayFind(4);
System.out.println("Corresponding index locations:" + i);
// modify
myArray.myArrayModify(1,9);

myArray.printAll();
}
``````

# I hope I can keep my original intention, keep writing and grow with you

This series of codes github address: https://github.com/shanggushenlong/Data_Structures_and_Algorithms_Java

Tags: github SpringBoot Java

Posted on Sat, 16 May 2020 21:13:16 -0400 by dr.maju