1

Fail-Fast Iterator を実装して、自分のリストからエントリを削除し、正しい動作でテストしたいと考えています。List は MyEntry 型の要素を保持します。

これらのエントリは、ジェネリック値と次のエントリへの参照を保持します。

class MyEntry<E>  {

    MyEntry<E> next;
    E o;

    MyEntry() {
        this(null, null);
    }

    MyEntry(E o) {
        this(o, null);
    }

    MyEntry(E o, MyEntry<E> e) {
        this.o = o;
        this.next = e;
    }
}

リスト自体は、pos エントリでその位置を追跡し、スタックのように動作します。ここで、UnsupportedOperation 例外をスローする代わりに、リストを反復処理してエントリを削除できるフェイルファスト イテレータを実装したいと考えています。

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

public class MyList<E> implements Cloneable, java.lang.Iterable {

    private MyEntry<E> begin;

    private MyEntry<E> pos;


    public MyList() {
        pos = begin = new MyEntry<E>();
    }


    public boolean empty() {
        return begin.next == null;
    }


    public boolean endpos() { 
        return pos.next == null;
    }


    public void reset() {
        pos = begin;
    }

    /**
     * Advances one step in this List.
     *
     * @throws NoSuchElementException if the last Entry of this List already has been reached.
     */
    public void advance() {
        if (endpos()) {
            throw new NoSuchElementException("Already at the end of this List");
        }
        pos = pos.next;
    }

    /**
     * Returns the actual element of this List.
     *
     * @return the actual element
     * @throws RuntimeException if the last Entry of this List already has been reached.
     */
    public E elem() {
        if (endpos()) {
            throw new NoSuchElementException("Already at the end of this List");
        }
        return pos.next.o;
    }

    /**
     * Inserts <code>o</code> in this List. It will be placed before the actual
     * element. After insertion the inserted element will become the actual
     * element.
     *
     * @param x the element to be inserted
     */
    public void add(E x) {
        MyEntry<E> newone = new MyEntry<E>(x, pos.next);

        pos.next = newone;
    }

    /**
     * Deletes the actual element of this List. The element after the actual
     * element will become the new actual element.
     *
     * @throws NoSuchElementException if the last Entry of this List already has been reached.
     */
    public void delete() {
        if (endpos()) {
            throw new NoSuchElementException("Already at the end of this List");
        }
        pos.next = pos.next.next;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            private MyEntry<E> it = null;

            @Override
            public boolean hasNext() {
                return pos != null;
            }
            @Override
            public E next() {
                if (it==null)
                    it = begin;
                else
                    it = it.next;
                return it.o;
            }
            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }

}

また、既に Iterator を List に実装しようとしていて、テスト クラスでテストしました。

public class MyListTest {

@Test
public void test() {

    MyList list = new MyList();

    list.add("a");
    list.add("b");
    list.add("c");

    while(list.iterator().hasNext()==true) {
        System.out.println(list);
    }

    list.iterator().remove();

    while(list.iterator().hasNext()==true) {
        System.out.println(list);
    }
}

}

しかし、出力は c をループし、エントリを削除することさえできません。

MyList を反復処理してエントリを削除できる Fast-Fail Iterator の正しい実装で行き詰まっています。単一の問題に分解できなかったので、Iterator を実装しようとしたときに出てきたいくつかの質問のリストを作成しました

  • Iterator は MyList クラスに実装する必要がありますか、それとも独自のクラスに実装する必要がありますか?
  • Advance() メソッドのように、Iterator を MyList の上に進めるにはどうすればよいですか?
  • テスト クラスの while ループは便利ですか、それとも代わりに別の方法を使用する必要がありますか?
4

0 に答える 0