java data structure - 1 array

1. Array

Arrays are the most widely used data storage structure and are built into most programming languages. Because arrays are so easy to understand, they are used as a starting point for introducing data structures. There are two types of arrays: unordered arrays and ordered arrays. An ordered array is the result of sorting an unordered array. In addition, the array needs to have the following requirements:

1. We usually assume that there are no holes in the array.

When we want to find an element in the array, after all the elements have been checked, but still not found, it means that the array does not contain this element. So how do we know that all elements have been checked? According to rule 1, as long as we ensure that all non-empty elements of the array are in the front of the array, then when we encounter the first empty element, it means that all elements have been searched.

2. When an element in an array is deleted, the positions of all subsequent elements in the array will be moved forward by one position.

For example, we delete the second element. In java, it is to remove the reference to this object from the array, as long as the corresponding position is set to null, but we can't do that here, because according to rule 1, we are looking for When the first null element is encountered, it is considered that all elements have been searched. For example, if you search for 5 now, you will not be able to find it. Therefore, to delete, all elements that follow must be moved forward by one position.

3. If it is an unordered array, when adding an element, it is always added to the last position of the array.

The insertion operation also needs to be satisfied. After insertion, there can still be no holes in the array, otherwise there will still be problems with the search. For example, there are still 2 positions to insert. If we insert at the last position, according to rule 1, after the search I can't find this element again.

4. If it is an ordered array, when adding an element to a certain position, the element at the current position and the element after it must be moved one position back.

When searching, if there are the same elements, there may be multiple matching values, so which one is returned? Or return them all? For convenience, we usually assume that there are no identical elements in the array, so just return the value on the first match.

Here is the code implementation:

public class Array<V> {
    private Object[] elements;
    private int size = 0;//数组元素的数量
    private int capacity;//数组的容量
    /**
     * 数组容量
     */
    public Array(int capacity){
        this.capacity = capacity;
        if (capacity <= 0) {
            throw new IllegalArgumentException("数组容量必须大于0");
        }
        elements = new Object[capacity];
    }
    /**
     * 插入元素
     */
    public void insert(V v){
        if (size == capacity -1) {//达到容量限制
            throw new IndexOutOfBoundsException("数组已达到最大容量");
        }
        elements[size++] = v;
    }
    /**
     * 删除元素
     */
    public boolean remove(V v){
        for (int i = 0; i < size; i++){
            if (elements[i].equals(v)) {
                elements[i] = null;//删除
                moveUp(i);
                size--;//元素数量-1
                return true;
            }
        }
        return false;
    }
    /**
     * 查找元素
     */
    public V find(V v){
        for (int i = 0; i < size; i++){
            if (elements[i].equals(v)) {
                return (V)elements[i];
            }
        }
        return null;
    }
    /**
     * 返回指定index的元素
     */
    public V get(int index){
        if (index > capacity - 1) {
            throw new IndexOutOfBoundsException("指定的索引超过数组容量");
        }
        return (V)elements[index];//null能强转吗
    }
    /**
     * 返回数组元素个数
     */
    public int size(){
        return size;
    }
    /**
     * 显示所有元素
     */
    public void showAll(){
        for (int i = 0; i< elements.length; i++){
            if (i < size) {
                System.out.print(elements[i] + "  ");
            }else {
                System.out.print("null" + "  ");
            }
        }
        System.out.println();
    }

    /** 
     * 删除元素后将所有元素都向前移动一位 
     */ 
    private void moveUp(int i) {
        while (i < size -1) {
            elements[i] = elements[++i];
        }
        elements[size -1] = null;
    }
}

In data structures and algorithms, the efficiency of algorithms is measured by time complexity and space complexity. The following is a brief introduction:

  • Operation ———————— Time Complexity
  • Insert —————— O(1)
  • Delete —————— O(n)
  • Find------------ O(n)

in:

O(1) means that this operation is not affected by the number of elements in the array. No matter how many elements there are in the array, the insertion operation can always be completed in one step.

O(N) means that this operation is affected by the number of data elements. The most intuitive feeling is that we can see the delete and search operations. There is a for loop to iterate all elements in the array, assuming that there are N elements in the array Element, we randomly find or delete a number. If you are lucky, you may find it once. If you are unlucky, it may be that all N elements have been iterated and still not found. According to the probability, an average of N/2 may be required. The average time complexity of delete and lookup operations is O(N) because the time complexity is ignoring constants.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325490209&siteId=291194637