简体   繁体   中英

How to remove element from private ArrayList in generic class

I have generic class MyArray where private member is ArrayList, and inside is implemented iterator.

In Main is given some MyArray with strings and I want to delete all "test" from it... Problem is in iterator which method remove doesn't work

Here is how class looks like:

public class MyArray<E> {
        private ArrayList<E> list;

        public MyArray() {
            list = new ArrayList<E>();
        }

        public int length() {    return list.size(); }

        public E at(int pos) {    return list.get(pos); }

        public void add(E val) {    list.add(val); }

        public void remove(int pos) {    list.remove(pos); }

        public class MyIterator implements Iterator<E>{
            int index;

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

            @Override
            public E next() {
                if (!hasNext())
                    throw new NoSuchElementException("no next value");
                E tmp = list.get(index);
                index++;
                return tmp;
            }

        }

        public Iterator<E> iterator() {
            return new MyIterator();
        }

    public static void main(String[] args) {        
        MyArray<String> b = new MyArray<String>();
        b.add("This");
        b.add("is");
        b.add("test");
        b.add("please");
        b.add("delete");
        b.add("all");
        b.add("test");

        Iterator<String> iter = b.iterator();
        while(iter.hasNext())
            System.out.println(iter.next());

        for(Iterator<String> i = b.iterator(); i.hasNext(); ) {
            String tmp = i.next();
            if (tmp.equals("test"))
                i.remove();
        }

        Iterator<String> ite = b.iterator();
        while(ite.hasNext())
            System.out.println(ite.next());
    }

}

Exception that I get is:

Exception in thread "main" java.lang.UnsupportedOperationException: remove
    at java.util.Iterator.remove(Unknown Source)
    at cas1.MyArray.main(MyArray.java:71)

You need to override remove() in your Iterator .

However, it'd be easiest to make your iterator() method return list.iterator() , rather than implementing it yourself:

    public Iterator<E> iterator() {
        return list.iterator();
    }

MyIterator that you defined doesn't override Iterator.remove() and the remove() defined in the Iterator interface is defined as a default method that throws UnsupportedOperationException :

default void remove() {
    throw new UnsupportedOperationException("remove");
}

So override it simply to remove effectively the iterated element.

You can rely on ArrayList.Itr code :

public void remove() {
    if (lastRet < 0)
        throw new IllegalStateException();
    checkForComodification();

    try {
        ArrayList.this.remove(lastRet);
        cursor = lastRet;
        lastRet = -1;
        expectedModCount = modCount;
    } catch (IndexOutOfBoundsException ex) {
        throw new ConcurrentModificationException();
    }
}

接口中的Iterator ,并且您需要在打算调用的MyIterator中实现每个Iterator方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM