0

我的项目有问题,我要求在包的元素上创建一个迭代器,我需要使用内部类来为密集包定义我的迭代器

这是我到目前为止所得到的,我不确定其他方法是否正常工作,但这里有(见链接,在这里粘贴代码时遇到问题,第一个计时器=()

public class DenseBag<T> extends AbstractCollection<T> {

    private Map<T, Integer> denseBagMap;
    private int size;  // Total number of elements in the bag
    transient int modCount;




    public DenseBag() { //DONE!
            size = 0;
            denseBagMap = new HashMap<T, Integer>();
    }

    public String toString() {
            throw new UnsupportedOperationException("YOU MUST IMPLEMENT THIS!");  
    }

    public boolean equals(Object o) {//DONE!
            if (o == this) {
                    return true;
            }
            if (!(o instanceof DenseBag)) {
                    return false;
            }
            DenseBag<T> dense = (DenseBag<T>) o;
            return size == dense.size;
    }

    public int hashCode() {//DONE!
            return this.denseBagMap.hashCode();
    }

    public Iterator<T> iterator() {
            throw new UnsupportedOperationException("YOU MUST IMPLEMENT THIS!");
    }

    public Set<T> uniqueElements() {
            throw new UnsupportedOperationException("YOU MUST IMPLEMENT THIS!");
    }

    public int getCount(Object o) {
            throw new UnsupportedOperationException("YOU MUST IMPLEMENT THIS!");
    }

    public T choose(Random r) {//DONE!
            ArrayList<T> keyArrayList = new ArrayList<T>();
            int index = 0;
            Iterator<T> it = denseBagMap.keySet().iterator();
            while (it.hasNext()){
                    T current = it.next();
                    while (index < denseBagMap.get(current)){
                            keyArrayList.add(current);
                            index++;
                    }
                    index = 0;
            }

            return keyArrayList.get(r.nextInt(keyArrayList.size())); }

    public boolean contains(Object o) {//DONE!
            return denseBagMap.containsKey(o);
    }

    public boolean add(T o) {//DONE!
            if (denseBagMap.containsKey(o)) {
                    denseBagMap.put(o, denseBagMap.get(o) + 1);
            } else {
                    denseBagMap.put(o, 1);
            }
            return true;
    }

    public boolean remove(Object o) {//DONE!
            if (o != null){
                    if (denseBagMap.containsKey(o)){
                            Integer newValue = denseBagMap.get(o)-1;
                            denseBagMap.put((T)o, newValue);
                            return true;
                    }
            }
            return false;
    }

    public int size() {//DONE?
            return size;
    }

}

*我设法根据最初的评论和我得到的结果来解决这个问题:*

private final class DenseBagIterator<E> implements Iterator<T> {
    private Iterator<Entry<T, Integer>> entrySetIterator;
    private int count = 0;
    private int max = 0;
    private T current = null;
    private int expectedModCount;

    public DenseBagIterator() {
        entrySetIterator =  denseBagMap.entrySet().iterator();
        expectedModCount = modCount;
    }

    public boolean hasNext() {
        if (count < max) {
            return true;
        } else {
            return entrySetIterator.hasNext();
        }
    }

    public T next() {
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
        if (count < max) {
            count++;
        } else {
            Entry<T, Integer> entrySet = entrySetIterator.next();
            current = entrySet.getKey();
            max = entrySet.getValue();
            count = 1;
        }
        return current;
    }

    @Override
    public void remove() {
        if (current == null)
            throw new IllegalStateException();
        if (modCount != expectedModCount)
            throw new ConcurrentModificationException();
        if (max > 1) {
            DenseBag.this.remove(current);
            count--;
        } else {
            entrySetIterator.remove();
            size--;
        }
        max--;
        expectedModCount = modCount;
    }

}
4

1 回答 1

2

我不会为你写你的迭代器。但基本思想是:

public Iterator<T> iterator() {
    return new DenseBagIterator();
}

private class DenseBagIterator implements Iterator<T> {
    // your implementation of the Iterator interface methods goes here
}

您的私有内部类可以访问所有私有数据和创建它的 DenseBag 对象的方法(DenseBag.this.whatever如果需要,使用 ),它也可以维护自己的状态数据(因此您可以让多个迭代器并行运行)。

开始编写所有必需的方法,并询问您何时遇到特定问题。:)

于 2013-04-09T21:49:08.563 回答