无法从数组中删除对象

时间:2016-05-18 15:06:28

标签: java arrays object arraylist

我有两节课。 一个类是MyListImpl。这里我有两种方法删除和添加。我像做ArrayList实现一样做smth。我的remove(Object o)方法无法正常工作。它给了我NullPointer。如何从我的数组中删除该对象?

        public class MyListImpl implements MyList, ListIterable {

        private Object[] array = new Object[8];

@Override
    public void add(Object e) {

        int mainArraySize = this.array.length;
        if (this.iterator < mainArraySize) {
            this.array[this.iterator] = e;
            this.iterator++;

        } else {
            Object[] arrayCopy = new Object[mainArraySize + 1];
            for (int i = 0; i < this.array.length; i++) {
                arrayCopy[i] = this.array[i];
            }
            arrayCopy[mainArraySize] = e;
            this.array = arrayCopy;
        }
    }




    @Override
        public boolean remove(Object o) {
            // Removing object from array
                int j = 0;
                Object[] arr = new Object[this.array.length-1];
                System.out.println(arr.length);
                for (int i = 0; i < this.array.length; i++) {
                    if (this.array[i].toString().equals(o.toString())!=true) {
                        arr[j] = this.array[i];
                        j += 1; 
                    }

                }

                return true;
            }

这是我的演示课程

public class Demo {

    public static void main(String[] args) {
        MyListImpl con = new MyListImpl();

        con.add("A");
        con.add("P");
        con.add("Z");
        con.add("1");
        con.add("53");
        con.remove("P");
}
}

0 个答案:

没有答案