數據結構--01(數組)

目錄

 

1.封裝泛型數組

2.動態數組

3.時間複雜度分析


1.封裝泛型數組

package com.wx1.test8;

public class Array<E> {

    private E[] data;
    private int size;

    // 構造函數,傳入數組的容量capacity構造Array
    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    // 無參數的構造函數,默認數組的容量capacity=10
    public Array() {
        this(10);
    }

    // 獲取數組的容量
    public int getCapacity() {
        return data.length;
    }

    // 獲取數組中的元素個數
    public int getSize() {
        return size;
    }

    // 返回數組是否爲空
    public boolean isEmpty() {
        return size == 0;
    }

    // 向所有元素後添加一個新元素
    public void addLast(E e) {
        add(size, e);
    }

    // 在所有元素前添加一個新元素
    public void addFirst(E e) {
        add(0, e);
    }

    // 在index索引的位置插入一個新元素e
    public void add(int index, E e) {

        if (size == data.length)
            throw new IllegalArgumentException("Add failed. Array is full.");

        if (index < 0 || index > size)
            throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");

        for (int i = size - 1; i >= index; i--)
            data[i + 1] = data[i];

        data[index] = e;

        size++;
    }

    // 獲取index索引位置的元素
    public E get(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal.");
        return data[index];
    }

    // 修改index索引位置的元素爲e
    public void set(int index, E e) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Set failed. Index is illegal.");
        data[index] = e;
    }

    // 查找數組中是否有元素e
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e))
                return true;
        }
        return false;
    }

    // 查找數組中元素e所在的索引,如果不存在元素e,則返回-1
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e))
                return i;
        }
        return -1;
    }

    // 從數組中刪除index位置的元素, 返回刪除的元素
    public E remove(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Remove failed. Index is illegal.");

        E ret = data[index];
        for (int i = index + 1; i < size; i++)
            data[i - 1] = data[i];
        size--;
        data[size] = null;
        return ret;
    }

    // 從數組中刪除第一個元素, 返回刪除的元素
    public E removeFirst() {
        return remove(0);
    }

    // 從數組中刪除最後一個元素, 返回刪除的元素
    public E removeLast() {
        return remove(size - 1);
    }

    // 從數組中刪除元素e
    public void removeElement(E e) {
        int index = find(e);
        if (index != -1)
            remove(index);
    }

    @Override
    public String toString() {

        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length));
        res.append('[');
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size - 1)
                res.append(", ");
        }
        res.append(']');
        return res.toString();
    }
}

2.動態數組

package com.wx1.test8;

import java.util.Arrays;

public class Array<E> {

    private E[] data;
    private int size;

    // 構造函數,傳入數組的容量capacity構造Array
    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }

    // 無參數的構造函數,默認數組的容量capacity=10
    public Array() {
        this(10);
    }

    // 獲取數組的容量
    public int getCapacity() {
        return data.length;
    }

    // 獲取數組中的元素個數
    public int getSize() {
        return size;
    }

    // 返回數組是否爲空
    public boolean isEmpty() {
        return size == 0;
    }

    // 向所有元素後添加一個新元素
    public void addLast(E e) {
        add(size, e);
    }

    // 在所有元素前添加一個新元素
    public void addFirst(E e) {
        add(0, e);
    }

    // 在index索引的位置插入一個新元素e
    public void add(int index, E e) {
        if (index < 0 || index > size)
            throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");

        if (size == data.length)
            resize(2 * data.length);

        for (int i = size - 1; i >= index; i--)
            data[i + 1] = data[i];
        data[index] = e;
        size++;
    }


    // 獲取index索引位置的元素
    public E get(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Get failed. Index is illegal.");
        return data[index];
    }

    // 修改index索引位置的元素爲e
    public void set(int index, E e) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Set failed. Index is illegal.");
        data[index] = e;
    }

    // 查找數組中是否有元素e
    public boolean contains(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e))
                return true;
        }
        return false;
    }

    // 查找數組中元素e所在的索引,如果不存在元素e,則返回-1
    public int find(E e) {
        for (int i = 0; i < size; i++) {
            if (data[i].equals(e))
                return i;
        }
        return -1;
    }

    // 從數組中刪除index位置的元素, 返回刪除的元素
    public E remove(int index) {
        if (index < 0 || index >= size)
            throw new IllegalArgumentException("Remove failed. Index is illegal.");

        E ret = data[index];
        for (int i = index + 1; i < size; i++)
            data[i - 1] = data[i];
        size--;
        data[size] = null;//讓對象失去對象的引用,從而變成遊離態的對象被垃圾回收機制回收。
        //如果元素少到容量的一半,就讓其縮容
        if (size <= data.length / 2)
            resize(data.length / 2);
        return ret;
    }

    // 從數組中刪除第一個元素, 返回刪除的元素
    public E removeFirst() {
        return remove(0);
    }

    // 從數組中刪除最後一個元素, 返回刪除的元素
    public E removeLast() {
        return remove(size - 1);
    }

    // 從數組中刪除元素e
    public void removeElement(E e) {
        int index = find(e);
        if (index != -1)
            remove(index);
    }

    private void resize(int newLength) {
        E[] newData = (E[]) new Object[newLength];
        data = Arrays.copyOf(data, newData.length);
    }

    @Override
    public String toString() {

        StringBuilder res = new StringBuilder();
        res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length));
        res.append('[');
        for (int i = 0; i < size; i++) {
            res.append(data[i]);
            if (i != size - 1)
                res.append(", ");
        }
        res.append(']');
        return res.toString();
    }
}

3.時間複雜度分析

       操作消耗的時間和數據規模之間的關係,

   O(1)就是這個操作的時間與輸入數據的規模沒有關係。

   

     

        均攤時間複雜度:平均每次add操作會有兩次基本操作。

        複雜度震盪

             

        

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章