1. 程式人生 > >資料結構之JAVA陣列重寫

資料結構之JAVA陣列重寫

package com.ws.scala;

public class Array {

private E[] data;
private int size; //實際有值長度

/**
 * 預設構造
 * 預設陣列容量指定為10
 */
public Array() {
    this(10);
}

/**
 * 指定陣列的容量
 *
 * @param capacity
 */
public Array(int capacity) {
    data = (E[])new Object[capacity];
}

/**
 * 賦值陣列
 *
 * @param data
 */
public Array(E[] data) {
    this.data = data;
}

public void addFirst(E e) {
    if (size == 0) {
        addLast(e);
        return;
    }
    data[0] = e;
}

public void addLast(E e) {
    if (isFull()) {
        //throw new IllegalArgumentException("add fail ,Array is full");
        //擴容為原來容量的2倍
        this.resetSize(this.data.length << 1);
    }
    data[size] = e;
    size++;
}

/**
 * 在指定索引地方插入
 *
 * @param index
 * @param e
 */
public void addInsert(int index, E e) {
    if (isFull()) {
        this.resetSize(this.data.length << 1);
        //throw new IllegalArgumentException("array size is full ,add fail");
    }

    if (index > size || index < 0) {
        throw new IllegalArgumentException("index out of range");
    }

    //倒著來
    for (int i = size; i > index; i--) {
        //後面的一個等於前面的一個
        data[i] = data[i - 1];
    }

    data[index] = e;
    size++;
}

public boolean isFull() {
    if (size >= data.length)
        return true;
    return false;
}


public E[] getData() {
    return data;
}

public void setData(E[] data) {
    this.data = data;
}

public int getSize() {
    return size;
}

public void setSize(int size) {
    this.size = size;
}

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("[");
    for (int i = 0; i < size; i++) {
        sb.append(data[i]);
        if (i != size - 1) {
            sb.append(",");
        }
    }
    sb.append("]");
    System.out.println(sb.toString());
    return null;
}

/**
 * 返回陣列的容量
 *
 * @return
 */
public int getCapacity() {
    return this.data.length;
}

/**
 * 傳入索引,獲取該位置元素
 *
 * @param index
 * @return
 */
public E getValue(int index) {
    if (index >= size || index < 0) {
        throw new IllegalArgumentException("parameter error");
    }
    return this.data[index];
}

/**
 * 傳入索引和值,替換原資料
 */
public void replace(int index, E value) {
    if (index >= size || index < 0) {
        throw new IllegalArgumentException("index out of bounds");
    }

    data[index] = value;
}

/**
 * 是否包含此索引
 *
 * @param index
 * @return
 */
public boolean containsIndex(int index) {
    return index < 0 || index >= size ? false : true;
}

/**
 * 是否包含此元素
 *
 * @param value
 * @return
 */
public boolean containsValue(E value) {

    for (int i = 0; i < size; i++) {
        if (data[i].equals(value)) {
            return true;
        }
    }
    return false;
}

/**
 * 返回第一個元素的索引
 *
 * @param value
 * @return
 */
public int findIndex(E value) {

    for (int i = 0; i < size; i++) {
        if (data[i].equals(value)) {
            return i;
        }
    }
    return -1;
}

/**
 * 查詢所有此值的索引
 *
 * @param value
 * @return
 */
public int[] findAll(E value) {
    int[] tempArray = new int[size];
    int tempSize = 0;

    for (int i = 0; i < size; i++) {
        if (data[i].equals(value)) {
            tempArray[tempSize] = i;
            tempSize++;
        }
    }


    int[] resultArr = new int[tempSize];

    for (int i = 0; i < tempSize; i++) {
        resultArr[i] = tempArray[i];
    }

    return resultArr;
}


/**
 * 刪除指定索引
 */
public boolean removeIndex(int index) {
    if (containsIndex(index)) {
        for (int i = index + 1; i < size; i++) {
            data[i - 1] = data[i];
        }
        size--;
        data[size] = null;
    }

    if (size <= (data.length >> 2) && (data.length >> 1)!= 0){
        resetSize(data.length >> 1);
    }

    return true;
}

/**
 * 刪除首元素
 *
 * @return 返回成功與否
 */
public boolean removeFirst() {
    return removeIndex(0);
}

/**
 * 刪除最後一個元素
 *
 * @return
 */
public boolean removeLast() {
    return removeIndex(this.size - 1);
}

/**
 * 刪除該值首次出現索引
 *
 * @param value
 * @return
 */
public boolean removeElement(E value) {
    int index = this.findIndex(value);
    return removeIndex(index);
}

/**
 * 刪除該值出現索引
 * @param value
 * @return
 */
public boolean removeAll(E value){
    int[] all = this.findAll(value);

    for (int i = 0 ;i < all.length ;i++){
        //this.removeElement(value);
        this.removeIndex(all[i]-i);
    }
    return true ;
}

/**
 * 擴容並拷貝至新陣列
 */
public void resetSize(int capacity){
    E [] newArr = (E[]) new Object[capacity];

    for (int i = 0 ;i < size ; i++){
        newArr[i] = this.data[i];
    }
    data = newArr;
}

}