List模拟

来源:互联网 发布:身份证号码查询软件 编辑:程序博客网 时间:2024/05/17 21:59

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package arthur.datastruct.programe;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 *
 * @author dell
 */
public class MyListText implements Iterable<Object> {

    private static final int DEFAULT_CAPACITY = 10;
    private int size;
    private Object[] items;

    /**
     * clear the data in items
     */
    public void clear() {
        for (int i = 0; i < items.length; i++) {
            items[i] = null;
        }

    }

    /**
     *return the size of MyList
     * @return int
     */
    public int size() {
        return this.size;
    }

    /**
     * to decide weather MyList is empty or not
     * @return boolean
     */
    public boolean isEmpty() {
        return 0 == size;
    }

    /**
     * get Object by id
     * @param id
     * @return
     */
    public Object getObject(int id) {
        if (id >= this.size() || id < 0) {
            throw new ArrayIndexOutOfBoundsException();
        }

        return items[id];
    }

    public MyListText() {
        items = new Object[DEFAULT_CAPACITY];
    }

    @Override
    public Iterator<Object> iterator() {
        return new ListIterator();
    }

    /**
     * to update the data at number id in arraylist
     * @param id   the number in arrayList
     * @param newObject the data you want to renew
     * @return
     */
    public Object set(int id, Object newObject) {
        if (id >= this.size() || id < 0) {
            throw new ArrayIndexOutOfBoundsException();
        }

        Object oldObject = this.items[id];
        items[id] = newObject;

        return oldObject;
    }

    /**
     * add new Object to items,before add we must make the space is enough,
     * if not ,double the items' space
     * @param object
     * @return
     */
    public boolean add(Object object) {

        //double the items' space
        if (size == items.length) {
            expandedLengthOfArray(items.length * 2);
        }
        items[size] = object;
        size++;

        return true;
    }

    /**
     * add a new data for items,before executing adddition,we,before
     * adding we must make the space is enough,
     * if not ,double the items' space
     * we should move
     * back the data,and the size should be ++
     * @param id
     * @param object
     */
    public void add(int id, Object object) {
        //double the space if items has no enough space
        if (items.length == this.size()) {
            this.expandedLengthOfArray(size * 2);
        }
        //move back data
        for (int i = this.size; i > id; i--) {
            items[i] = items[i - 1];
        }
        items[id] = object;
        this.size++;
    }

    /**
     *
     * to increase the length of array,
     * if the newCapacity is less than the size,then
     * this operation is not reasonable, else when executive this method, we must
     * Copy  the original data to make sure the data are not lost
     * @param newCapacity
     */
    public void expandedLengthOfArray(int newCapacity) {
//        copy data
        Object[] oldItems = this.items;

        items = new Object[newCapacity];

        System.arraycopy(oldItems, 0, items, 0, this.size);
    }

    /**
     * remove the data at index position in items,we must make sure the data left
     * afther remove move forward,and the total number of size will be --
     * @param index
     * @return
     */
    public Object remove(int index) {
        Object removeObject = items[index];
        for (int i = index; i < this.size(); i++) {
            items[i] = items[i + 1];
        }
        this.size--;
        return removeObject;
    }

    private class ListIterator implements Iterator<Object> {

        private int index;

        @Override
        public boolean hasNext() {
            return this.index < size();
        }

        @Override
        public Object next() {
            if (!this.hasNext()) {
                throw new NoSuchElementException();
            }
            return items[index++];
        }

        @Override
        public void remove() {
            MyListText.this.remove(--index);
        }
    }
}