使用迭代器创建系列
Create a Series using Iterators
我想写一个 class(称为 Seii),基本上是从 s0 开始的整数序列。 s0 在构造函数中设置:
se + 1 = 3*(se/2)
问题是:for 循环应该能够遍历此 class 的对象并吐出序列的元素(没有起始编号 s0)。此外,序列以大于 42 的第一个元素结束。
例如:
for(int i:new Seii(2)){
System.out.println(i)
给出:
3,4,6,9,10,15,16,24,36,54
我想用迭代器来做。有人可以帮我吗?
我的想法是重写 next() 方法,以便它计算序列的下一个元素,但我对这个逻辑一无所知。
public class Seii<T> implements Iterator {
private ArrayList<Integer> list = new ArrayList<>();
Iterator<Integer> it = list.iterator();
private final int size;
public Seii(int size) {
this.size = size;
}
int seii = 0;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public Object next() {
if ((size % 2) == 0) {
seii = 3 * (seii/2);
return seii;
}
}
}
}
这是我的实现。
您的 Seii
class 应该实现 Iterable<Integer>
而不是 Iterator
,因为这是增强的 for 循环所需的接口。它将有一个 iterator
方法,该方法 returns 实现 Iterator<Integer>
接口的 class 实例。
您不需要存储序列,因此可以从您的实现中删除数组列表。你只需要最后一个值,可以在构造函数中设置:
// This is a wrapper class that constructs iterators.
// It is used for plugging in your code into enhanced "for" loop
class Seii implements Iterable<Integer> {
private int current;
private int max;
public Seii(int current, int max) {
this.current = current;
this.max = max;
}
@Override
public Iterator<Integer> iterator() {
return new SeiIterator(current, max);
}
}
// This is the actual iterator that maintains state
// and produces the desired sequence.
class SeiIterator implements Iterator<Integer> {
private int current;
private int max;
public SeiIterator(int current, int max) {
this.current = current;
this.max = max;
}
@Override
public boolean hasNext() {
return current < max;
}
@Override
public Integer next() {
current = (3*current)/2;
return current;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
请注意,为了在增强的 for
循环中使用迭代器,您需要将其包装在 Iterable<Integer>
.
中
Seii
应该实现 Iterable<Integer>
, which will allow it to support the enhanced for loop syntax. The easiest way of doing that, IMHO, is just to have an inner Iterator
class 实现你的逻辑:
public class Seii implements Iterable<Integer> {
private class SeiiIterator implements Iterator<Integer> {
@Override
public boolean hasNext() {
return value <= 42;
}
@Override
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
value = 3 * (value / 2);
return value;
}
}
private int value;
public Seii(int value) {
this.value = value;
}
@Override
public Iterator<Integer> iterator() {
return new SeiiIterator();
}
}
我想写一个 class(称为 Seii),基本上是从 s0 开始的整数序列。 s0 在构造函数中设置:
se + 1 = 3*(se/2)
问题是:for 循环应该能够遍历此 class 的对象并吐出序列的元素(没有起始编号 s0)。此外,序列以大于 42 的第一个元素结束。
例如:
for(int i:new Seii(2)){
System.out.println(i)
给出:
3,4,6,9,10,15,16,24,36,54
我想用迭代器来做。有人可以帮我吗? 我的想法是重写 next() 方法,以便它计算序列的下一个元素,但我对这个逻辑一无所知。
public class Seii<T> implements Iterator {
private ArrayList<Integer> list = new ArrayList<>();
Iterator<Integer> it = list.iterator();
private final int size;
public Seii(int size) {
this.size = size;
}
int seii = 0;
@Override
public boolean hasNext() {
// TODO Auto-generated method stub
return false;
}
@Override
public Object next() {
if ((size % 2) == 0) {
seii = 3 * (seii/2);
return seii;
}
}
}
}
这是我的实现。
您的 Seii
class 应该实现 Iterable<Integer>
而不是 Iterator
,因为这是增强的 for 循环所需的接口。它将有一个 iterator
方法,该方法 returns 实现 Iterator<Integer>
接口的 class 实例。
您不需要存储序列,因此可以从您的实现中删除数组列表。你只需要最后一个值,可以在构造函数中设置:
// This is a wrapper class that constructs iterators.
// It is used for plugging in your code into enhanced "for" loop
class Seii implements Iterable<Integer> {
private int current;
private int max;
public Seii(int current, int max) {
this.current = current;
this.max = max;
}
@Override
public Iterator<Integer> iterator() {
return new SeiIterator(current, max);
}
}
// This is the actual iterator that maintains state
// and produces the desired sequence.
class SeiIterator implements Iterator<Integer> {
private int current;
private int max;
public SeiIterator(int current, int max) {
this.current = current;
this.max = max;
}
@Override
public boolean hasNext() {
return current < max;
}
@Override
public Integer next() {
current = (3*current)/2;
return current;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
请注意,为了在增强的 for
循环中使用迭代器,您需要将其包装在 Iterable<Integer>
.
Seii
应该实现 Iterable<Integer>
, which will allow it to support the enhanced for loop syntax. The easiest way of doing that, IMHO, is just to have an inner Iterator
class 实现你的逻辑:
public class Seii implements Iterable<Integer> {
private class SeiiIterator implements Iterator<Integer> {
@Override
public boolean hasNext() {
return value <= 42;
}
@Override
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
value = 3 * (value / 2);
return value;
}
}
private int value;
public Seii(int value) {
this.value = value;
}
@Override
public Iterator<Integer> iterator() {
return new SeiiIterator();
}
}