是否有类似于C#/.NET的
IEnumerable<int> range = Enumerable.Range(0, 100); //.NET
在Java中怎么做?
是否有类似于C#/.NET的
IEnumerable<int> range = Enumerable.Range(0, 100); //.NET
在Java中怎么做?
在Java8以后,可以使用java.util.stream.IntStream.range(int startInclusive, int endExclusive)
Java中没有这样的东西,但你可以像这样实现:
import java.util.Iterator;
public class Range implements Iterable<Integer> {
private int min;
private int count;
public Range(int min, int count) {
this.min = min;
this.count = count;
}
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
private int cur = min;
private int count = Range.this.count;
public boolean hasNext() {
return count != 0;
}
public Integer next() {
count--;
return cur++; // first return the cur, then increase it.
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}
例如,您可以通过以下方式使用Range:public class TestRange {
public static void main(String[] args) {
for (int i : new Range(1, 10)) {
System.out.println(i);
}
}
}
如果你不喜欢直接使用new Range(1, 10)
,你可以使用工厂类来实现:
public final class RangeFactory {
public static Iterable<Integer> range(int a, int b) {
return new Range(a, b);
}
}
这里是我们的工厂测试:
public class TestRangeFactory {
public static void main(String[] args) {
for (int i : RangeFactory.range(1, 10)) {
System.out.println(i);
}
}
}
Java采取的方法是认为有无限种组合方式,因此为什么要特别对待其中几种组合。有了正确的构建块,其他所有东西都可以轻松构建(这也是Unix哲学)。
其他语言API(例如C#和Python)采取了更谨慎的方法,它们确实选择了一些易于操作的事物,但仍允许更深奥的组合。
Java方法存在问题的典型示例可以在Java IO库中看到。创建文本输出文件的规范方法是:
BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"));
out = file("out.txt","w")
public class Enumerable extends ArrayList<Integer> {
public Enumerable(int min, int max) {
for (int i=min; i<=max; i++) {
add(i);
}
}
}
然后使用迭代器从最小值到最大值(包括最小值和最大值)获取整数序列。
编辑
正如sepp2k所提到的-上面的解决方案快速,简单实用,但有一些严重的问题(不仅空间复杂度为O(n),而应该是O(1))。为了更严格地模拟C#类,我宁愿编写一个自定义的Enumerable类来实现Iterable和自定义迭代器(但不是在这里和现在;))。
GetEnumerator
或在foreach
中使用它,IEnumerable
将被实际值填充。MSDN文档:http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx - Powerlord
next()
的契约是在迭代器没有更多元素时抛出NoSuchElementException
,因此我的建议是在next()
中首先添加if (!hasNext()) throw new NoSuchElementException()
。 :) - Ibrahim Arief