我正在用 Java 实现一个自定义生成器,用于螺旋,我想知道哪种方法更好:
public class SpiralIterator<E> implements Iterator<List<E>> {
private final int parameters;
public SpiralIterator(int parameters) {
this.parameters = parameters;
}
@Override
public boolean hasNext() {
return true;
}
@Override
public List<E> next() {
//...
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
或者
public class SpiralIterator<E> implements Iterator<E[]> {
private final int parameters;
public SpiralIterator(int parameters) {
this.parameters = parameters;
}
@Override
public boolean hasNext() {
return true;
}
@Override
public E[] next() {
//...
}
@Override
public void remove() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
假设您使用SpiralIterator<Integer> spiralIterator = new SpiralIterator<>(2);
.
然后你通过 获得新元素,然后分别用 和List<E> next = spiralIterator.next();
获得元素。在另一种情况下,您通过检索它并随后通过and获取它。E e1 = next.get(0);
E e2 = next.get(1);
E[] next = spiralIterator.next();
E e1 = next[0];
E e2 = next[1];
这两种方法中的哪一种更受欢迎?
也可以“允许”(根据约定)将其称为 aSpiralGenerator
吗?