A simple introduction to array in Java data structure and algorithm

Time:2020-1-21

I have no idea about this block all the time, and I don’t understand it very well, but I just know a general idea. Recently, I felt a little interested in data structure and algorithm, so I decided to look at these structures and algorithms in detail as much as possible;

What is the data structure and algorithm? Now I can’t say. Let’s sum up when I’ve almost learned!

I borrowed a picture at will. The so-called data structure is the following. Let’s take a look at it one by one

1. Basic usage of array

You should be familiar with arrays. After learning the eight basic types of Java, the next one is to learn arrays. The biggest feature of arrays is that in addition to object arrays, other arrays can only store the same data type, and we need to specify a certain length at the beginning of specifying arrays. Once the inserted data exceeds this length, an error will be reported. In fact, because Array shortcomings are too big, so according to the basis of the array design out of the collection, collection later said..

There are two ways to use this array. Take the object array as an example. You can put anything in this array, and then use arrays.tostring (xxx) to print all the elements in the array;

  

Any data type has its corresponding array, such as int [], string [], Boolean [], etc

2. Simple implementation of adding, deleting, modifying and querying arrays

·It’s troublesome to use arrays, and we need to assign values one by one slowly. It’s not very convenient for us to operate data, so let’s implement the simplest addition, deletion, modification and query of our own arrays at will!

package com.wyq.thread;

public class MyArray {
  private int[] arr;
  //The number of data currently stored in an array, also known as the effective length of the array
  private int eles;
  
  //The maximum number that can be accommodated in an array
  private int length;
  
  // note that these two constructors invoke parametric constructs in the reference structure and set the default array length.
  public MyArray() {
    this(10);
  }
  public MyArray(int length){
    eles = 0;
    this.length = length;
    arr = new int[length];
  }
  //Get the number of actual data stored in the current array
  public int size(){
    return eles;
  }
  //Show data in current array
  public void show(){
    System. Out. Print ("all numbers in the array are:");
    for (int i = 0; i < arr.length; i++) {
      System.out.print(arr[i]+" ");
    }
    System.out.println();
  }
  
  //Add data to an array
  public boolean add(int num){
    //If the current array is full and data is added to it, an exception will be thrown and false will be returned, and subsequent operations will continue
    //We don't think about expanding this array here
    if (length==eles) {
      try {
        Throw new exception ("sorry, the array is full, you'd better not save it! "";
      } catch (Exception e) {
        e.printStackTrace();
      }
      return false;
    }else{
      arr[eles] = num;
      eles++;
      return true;
    }
  }
  //Check whether there is a XXX data in the array, return its index value if there is one, and return null if not. In fact, you can also catch exceptions. You can try it yourself
  public Integer find(int value){
    if (arr!=null) {
      for (int i = 0; i < arr.length; i++) {
        if (arr[i]==value) {
          return i;
        }
      }
    }
    return null;
  }
  //Delete XX data in the array. There will be many judgments here. It can be said that this method is the most troublesome one in the array
  //1. First call the query method to see if there is any data in the array that we want to delete. If there is no direct return of - 1, go to the second step
  //2. If the data to be deleted is at the end of the array, directly reduce the effective length of the array by one. If it is not at the end, enter the third step
  //3. To delete the middle position of an array, it is impossible to directly copy this data to 0 or null. We only need to move all the data behind this position forward
  //Just move one position, and then the last position of the array will be empty. Let's reduce the effective length of the array by one
  public int delete(int value){
    Integer find = find(value);
    if (find!=null) {
      if (find==eles-1) {
        eles--;
      }else{
        for (int i = find; i < arr.length-1; i++) {
          if (arr[i]==value) {
            arr[i]=arr[i+1];
          }
        }
        eles--;
      }
      
    }
    return -1;  
    
  }
  //To update the data, first call the query method to find the location of the data, and then directly assign a value
  public boolean update(int index,int value){
    Integer find = find(index);
    if (find!=null) {
      arr[index]=value;
      return true;
    }
    return false;
  }


  public static void main(String[] args) {
    //Since the array length is not specified, the default 10
    MyArray array = new MyArray();
    
    for (int i = 0; i < 10; i++) {
      array.add(i);
    }
    //Note that at this time, because the array is full, we will add data, and an exception will be reported
    array.add(222);
    array.show();
    array.delete(5);
    System. Out. Println ("actual size of array after deleting data:" + array. Size());
    array.add(100);
    array.update(0, 10);
    array.find(9);
    array.show();
    
  }
}

I don’t know if you have found that the simplest array we implemented is similar to the collection, that is, the ArrayList. Go to the source code and you will find that this is the ugliest version of ArrayList, ha ha ha! Just to achieve the simplest function!

Recommended Today

PHP realizes UnionPay business H5 payment

UnionPay business H5 payment interface document: document address 1: H5 payment interface address: 1: Alipay payment Test address: http://58.247.0.18:29015/v1/netpay/trade/h5-pay Official address: https://api-mop.chinaums.com/ 2: UnionPay payment Test address: http://58.247.0.18:29015/v1/netpay/uac/order Official address: https://api-mop.chinaums.com/ 2: Basic parameters required by the interface The interface uses get parameters. After the interface parameters are directly put into the interface address, the […]