gpt4 book ai didi

JAVA:嵌套在公共(public)类的迭代器中

转载 作者:行者123 更新时间:2023-12-01 16:59:31 27 4
gpt4 key购买 nike

因此,对于一个学校项目,我需要创建一个实现 Iterator 接口(interface)的私有(private)方法。我有我的方法 SetIterator,它看起来像这样:

  private class SetIterator implements Iterator<T> {
private T[] items;
private int count;
private int current;

private SetIterator(T[] elements, int size) {
items = elements;
count = size;
current = 0;
}

private boolean hasNext() {
return (current < count);
}

private T next() {
if (!hasNext()) {
throw new NoSuchElementException();
}

return items[current++];
}

但是,我无法编译,因为收到此错误消息:

ArraySet.java:311: error: next() in ArraySet.SetIterator cannot implement next() in Iterator
private T next() {
^
attempting to assign weaker access privileges; was public
where T,E are type-variables:
T extends Comparable<? super T> declared in class ArraySet
E extends Object declared in interface Iterator
ArraySet.java:307: error: hasNext() in ArraySet.SetIterator cannot implement hasNext() in Iterator
private boolean hasNext() {
^
attempting to assign weaker access privileges; was public

我以前从未真正遇到过这个问题,所以任何帮助将不胜感激。

最佳答案

迭代器是一个接口(interface)。因此,它的方法是公开的。您无法实现该方法并将其设置为私有(private)。

将可见性更改为公开

public boolean hasNext() {
return (current < count);
}

关于JAVA:嵌套在公共(public)类的迭代器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28760749/

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