gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:07:37 24 4
gpt4 key购买 nike

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

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com