数组

来源:互联网 发布:淘宝买家id是什么 编辑:程序博客网 时间:2024/06/03 19:04
public class MyArray<T> {    private Object array[];    private int elements;    public MyArray() {        array = new Object[50];    }    public MyArray(int maxSize) {        array = new Object[maxSize];    }    /**     *      * @Title: add     * @Description: 添加数据     * @param value     */    public void add(T value) {        array[elements] = value;        elements++;    }    /**     *      * @Title: display     * @Description: 显示数据     */    public void display() {        System.out.print("[ ");        for (int i = 0; i < elements; i++) {            System.out.print(array[i] + " ");        }        System.out.println("]");    }    /**     *      * @Title: size     * @Description: 数据中的有效数据长度     * @return     */    public int size() {        return elements;    }    /**     *      * @Title: search     * @Description: 查找     * @param index     * @return     */    public Object search(int index) {        if (index < 0 || index >= elements) {            throw new ArrayIndexOutOfBoundsException();        } else {            return array[index];        }    }    /**     *      * @Title: delete        * @Description: 删除        * @param index     */    public void delete(int index) {        if (index < 0 || index >= elements) {            throw new ArrayIndexOutOfBoundsException();        } else {            for(int i=index;i<elements - 1;i++) {                array[i] = array[i + 1];            }            elements --;        }    }}
原创粉丝点击