int数组操作类(不是list,自动扩张的数组)

public class OptIntArrayList {
    private int[] elements;
    private int size;
    private boolean dummy;

    public static OptIntArrayList DUMMY = new OptIntArrayList(0, true);

    public static String OUT_OF_SIZE;

    public OptIntArrayList() {
        this(16);
    }

    public OptIntArrayList(int capacity) {
        this.elements = new int[capacity];
        this.size = 0;
    }

    public OptIntArrayList(int capacity, boolean dummy) {
        this.elements = new int[capacity];
        this.size = 0;
        this.dummy = dummy;
    }

    public OptIntArrayList(int capacity, int size) {
        this.elements = new int[capacity];
        this.size = size;
        assert size <= capacity;
    }

    public OptIntArrayList(int[] values) {
        this.size = values.length;
        this.elements = new int[size];
        System.arraycopy(values, 0, elements, 0, size);
    }

    public OptIntArrayList(int[] values, int size) {
        this.size = size;
        this.elements = new int[size];
        System.arraycopy(values, 0, elements, 0, size);
    }

    public OptIntArrayList(int[] values, int index, int size) {
        this.size = size;
        this.elements = new int[size];
        System.arraycopy(values, index, elements, 0, size);
    }

    public OptIntArrayList(OptIntArrayList another) {
        this.size = another.size;
        this.elements = new int[size];
        System.arraycopy(another.elements, 0, elements, 0, size);
    }

    public static OptIntArrayList wrap(int[] values) {
        OptIntArrayList list = new OptIntArrayList();
        list.elements = values;
        list.size = values.length;
        return list;
    }

    public static OptIntArrayList wrap(int[] values, int size) {
        OptIntArrayList list = new OptIntArrayList();
        list.elements = values;
        list.size = size;
        return list;
    }

    public int[] toIntArray() {
        int[] copyElements = new int[size];
        System.arraycopy(elements, 0, copyElements, 0, size);
        return copyElements;
    }

    public boolean isDummy() {
        return dummy;
    }

    public int getInt(int index) {
        return elements[index];
    }

    public int get(int index) {
        return elements[index];
    }

    public int removeInt(int index) {
        // check index to see whether it's legal
        if (index >= size) {
            // can not be larger than size
            throw new IndexOutOfBoundsException(OUT_OF_SIZE);
        }
        int old = elements[index];
        size--;
        if(index != size) {
            System.arraycopy(elements, index + 1, elements, index, size - index);
        }
        return old;        
    }

    public int[] elements() {
        return elements;
    }

    public int set(int index, int newValue) {
        // check index to see whether it's legal
        if (index >= size) {
            // can not be larger than size
            throw new IndexOutOfBoundsException(OUT_OF_SIZE);
        }
        int oldValue = elements[index];
        elements[index] = newValue;
        return oldValue;
    }

    public int size() {
        return size;
    }

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

    public int capacity() {
        return elements.length;
    }

    public void add(int index, int value) {
        // check index to see whether it's legal
        if (index > size) {
            // can not be larger than size
            throw new IndexOutOfBoundsException(OUT_OF_SIZE);
        }
        if (size == elements.length) {
            int oldLength = size;
            int newLength = Math.max(size + 1,  (size + (size >> 1)));
            if (newLength == 0) {
                newLength = 1;
            }
            if (newLength == oldLength) {
                newLength = oldLength + 1;
            }
            int[] newElements = new int[newLength];
            System.arraycopy(elements, 0, newElements, 0, size);
            this.elements = newElements;
        } 
        if(index != size) {
            System.arraycopy(elements, index, elements, index + 1, size - index);
        }
        elements[index] = value;       
        size++;
    }

    public boolean add(int value) {
//        add(size, value);
        if (size == elements.length) {
            int oldLength = size;
            int newLength = Math.max(size + 1,  (size + (size >> 1)));
            if (newLength == 0) {
                newLength = 1;
            }
            if (newLength == oldLength) {
                newLength = oldLength + 1;
            }
            int[] newElements = new int[newLength];
            System.arraycopy(elements, 0, newElements, 0, size);
            this.elements = newElements;
        } 
        elements[size] = value;       
        size++;
        return true;
    }

    public void addNulls(int addSize) {
        int newSize = size + addSize;
        if (newSize > elements.length) {
            int oldLength = size;
            int newLength = Math.max(size + addSize,  (size + (size >> 1)));
            if (newLength == 0) {
                newLength = 1;
            }
            if (newLength == oldLength) {
                newLength = oldLength + addSize;
            }
            int[] newElements = new int[newLength];
            System.arraycopy(elements, 0, newElements, 0, size);
            this.elements = newElements;
        } 
        size = newSize;
    }

    public void addAll(int index, OptIntArrayList another) {
        int addSize = another.size();
        addElements(index, another.elements, 0, addSize);
    }

    public void addAll(OptIntArrayList another) {
        int addSize = another.size();
        addElements(size, another.elements, 0, addSize);
    }

    public void addElements(int index, int[] addElements, int offset, int length) {
        int newSize = size + length;
        if (newSize > elements.length) {
            int newLength = Math.max(newSize, (size + (size >> 1)));
            int[] newElements = new int[newLength];
            System.arraycopy(elements, 0, newElements, 0, size);
            this.elements = newElements;
        }
        if(index != size) {
            System.arraycopy(elements, index, elements, index + length, size - index);
        }
        System.arraycopy(addElements, offset, elements, index, length); 
        this.size = newSize;        
    }

    public int remove(int index)
    {
        return removeInt(index);
    }    

    public void trim() {
        int elementsSize = elements.length;
        if (size == elementsSize) {
            // no need to trim
            return;
        }
        int[] newElements = new int[size];
        // make a copy
        System.arraycopy(elements, 0, newElements, 0, size);
        // assign
        this.elements = newElements;
    }

    public void reset() {
        this.size = 0;
    }

    public void clear() {
        this.elements = new int[16];
        this.size = 0;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int indexOf(int o) {
        for (int i = 0; i < size; i++) {
            if (Functions.equals(o, elements[i])) {
                return i;
            }
        }
    return -1;
    }

    public String toString() {
        StringBuilder s = new StringBuilder();
        s.append("[");
        for (int i = 0; i < size; i++) {
            if(i > 0) {
                s.append(", ");
            }
            int e = elements[i];
            s.append("" + e);
        }
        s.append("]");
        return s.toString();
    }


}

你可能感兴趣的:(util)