java - 如何为包编写迭代器? (需要内部类)

标签 java dictionary project computer-science

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

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

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;
    }

}

最佳答案

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

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

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

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

开始对所有必需的方法进行编码,并在遇到特定问题时询问。 :)

关于java - 如何为包编写迭代器? (需要内部类),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15913141/

相关文章:

java - 我的网络驱动程序不理解 xpath

java - 输入流和输出流抽象方法

c++ - 删除 C++ 映射中包含单个值的键

c# - 尽管 Comparer.Equals 返回 true,但字典不包含键

考虑 x^i+y^i=z^i x<=y<=z<=m 和 2<=i<=n (m 和 n 是输入) m 可以在 5 到 100 之间变化 n 可以在 2 到 100 之间变化

java - 有没有办法从 Java Eclipse 项目中导出 Makefile?

java - cqengine IndexedCollection 添加导致空指针

java - 更新字符串字段,将 mongodb 中的另一个字符串与 spring-data 连接起来

python - 将字典转换为 Pandas 数据框

java - 在 Java 项目中引用外部模块的最佳实践