简体   繁体   English

如何实现迭代器?

[英]How to implement an iterator?

I'm having a problem with my class, which I cannot resolve. 我的课程有问题,无法解决。 I want to implement an iterator to my Interval1 class. 我想为我的Interval1类实现一个迭代器。 Here is my code: 这是我的代码:

public class Interval1 {
    private double first;
    private double last;
    private double step;
    private IntervalIterator e;

    public Interval1(double first, double last, double step,  IntervalIterator e) {
        //chequear los datos
        this.first = first;
        this.last = last;
        this.step = step;
        this.e = new IntervalIterator();
    }

    public  double at(int index) {
        checkIndex(index);
        return first + index*step;
    }

    private void checkIndex(int index) {
        if(index < 0 || index >= size())
            throw new IndexOutOfBoundsException("Invalid Index: " + index);
    }

    public int size() {
        return (int) ((last - first) / step);
    }

    public IntervalIterator e() {
        for (Double i : this)
            System.out.println(i);
        return e;
    }
}

This is the class IntervalIterator that I'm using and this give an error such in size() and at : 这是我正在使用的IntervalIterator类,它给出了size()at等错误:

public class IntervalIterator implements Iterator<Double> {
    private int index = 0;
    private Double at;

    public boolean hasNext() {
        return index < size();
    }

    public Double next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        return at(index++);
    }

    public void remove() {
        if (Interval1 <= 0) {
            return throw new UnsupportedOperationException("remove");
        } else {
            if (Interval1 >= 0) {
                //chekear el array e eliminarlo
            }
        }
    }
}

I'd use java8 streams to return an Iterator<Double> : 我将使用java8流返回Iterator<Double>

public class Interval
    implements Iterable<Double> {

    private final double first;
    private final double last;
    private final double step;
    private final long size;

    public Interval(double first, double last, double step) {
        this.first = first;
        this.last = last;
        this.step = step;
        this.size = (long) ((last - first) / step) + 1;
    }

    @Override
    public Iterator<Double> iterator() {
        return DoubleStream.iterate(this.first, n -> n + this.step)
            .limit(this.size)
            .iterator();
    }

    // TODO getters
}

I've added the final modifier to the fields and made the Interval class implement Iterable<Double> , so that it can be iterated. 我将final修饰符添加到字段中,并使Interval类实现Iterable<Double> ,以便可以对其进行迭代。 I've also fixed the calculation for size (it needed to be incremented by one). 我还修复了size计算(需要加一)。

I've used the DoubleStream.iterate() method , which receives a seed and a function that receives the current element of the stream as an input and returns the following element (for this I've added step to the current element). 我使用了DoubleStream.iterate()方法 ,该方法接收一个种子和一个函数,该函数接收流的当前元素作为输入并返回以下元素(为此,我已将step添加到当前元素中)。 I also had to use the DoubleStream.limit() method , because the stream generated by DoubleStream.iterate() is infinite. 我还必须使用DoubleStream.limit()方法 ,因为DoubleStream.iterate()生成的流是无限的。

Usage: 用法:

Interval interval = new Interval(3.0, 9.0, 1.5);

for (double n : interval) {
    System.out.println(n);
}

The above code generates the following output: 上面的代码生成以下输出:

3.0
4.5
6.0
7.5
9.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM