如何在类growArray中测试delete()

时间:2015-06-14 14:26:10

标签: java arrays collections

我正在学习Java中的Growing Array,我在下面的代码中实现了delete()方法。

现在我想为示例数组[1,2,3,4,5,6,7]

测试此方法

我需要用Main方法写什么?

import java.util.Arrays;

public abstract class GrowingArray {

    protected Object[] array;
    protected static final int primaryQty = 10;
    protected static final int secondaryQty = 5;
    protected int index = 0;

    public GrowingArray() {
        array = new Object[primaryQty];
    }

    public GrowingArray(int size) {
        array = new Object[size];
    }

    protected void grow() {
        int oldsize = array.length;
        int newsize = oldsize + secondaryQty;
        Object[] loc = new Object[newsize];
        for (int i = 0; i < oldsize; i++)
            loc[i] = array[i];
        array = loc;
    }

    public Object get(int at) {
        return array[at];
    }

    public int getLength() {
        return array.length;
    }

    public void add(Object obj) {
        if (index < array.length)
            array[index++] = obj;
        else {
            grow();
            array[index++] = obj;
        }
    }

    public void delete(int x) {

        for (int i = x; i < array.length; i++) {
            if (i == array.length - 1) {
                array[i] = null;
                System.out.println(array.toString());
            } else {
                array[i] = array[i + 1];
                System.out.println(array.toString());
            }
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof GrowingArray) {
            return Arrays.equals(array, ((GrowingArray) obj).array);
        }
        return false;
    }

    @Override
    public String toString() {
        return Arrays.toString(array);
    }

    public static void main(String args[]) {

    //test   ?????

    }

}

1 个答案:

答案 0 :(得分:0)

你的课是抽象的。从类定义中删除摘要。