【发布时间】:2011-02-26 09:09:36
【问题描述】:
有没有类似 C#/.NET 的东西
IEnumerable<int> range = Enumerable.Range(0, 100); //.NET
在 Java 中?
【问题讨论】:
标签: c# java .net language-features enumerable
有没有类似 C#/.NET 的东西
IEnumerable<int> range = Enumerable.Range(0, 100); //.NET
在 Java 中?
【问题讨论】:
标签: c# java .net language-features enumerable
你可以继承一个 Arraylist 来达到同样的效果:
public class Enumerable extends ArrayList<Integer> {
public Enumerable(int min, int max) {
for (int i=min; i<=max; i++) {
add(i);
}
}
}
然后使用迭代器得到一个从min到max(都包括)的Integers序列
编辑
正如 sepp2k 所提到的 - 上面的解决方案是快速、肮脏和实用的,但有一些严重的撤回(不仅在空间上 O(n),而它应该有 O(1))。为了更认真地模拟 C# 类,我宁愿编写一个自定义的 Enumerable 类,它实现了 Iterable 和一个自定义迭代器(但不是现在;))。
【讨论】:
Java 中没有对此的内置支持,但是自己构建它非常容易。总的来说,Java API 提供了实现此类功能所需的所有位,但不会开箱即用地组合它们。
Java 采用的方法是有无数种组合方式,所以为什么要优先考虑一些组合而不是其他组合。有了正确的构建块集,其他一切都可以轻松构建(这也是 Unix 哲学)。
其他语言 API(例如 C# 和 Python)采取更加谨慎的观点,他们确实选择了一些使事情变得非常简单的东西,但仍然允许更深奥的组合。
在Java IO 库中可以看到Java 方法问题的典型示例。为输出创建文本文件的规范方法是:
BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"));
Java IO 库使用Decorator Pattern,这对于灵活性来说是一个非常好的主意,但您肯定经常需要缓冲文件吗?将其与 Python 中的等价物进行比较,这使得典型用例非常简单:
out = file("out.txt","w")
【讨论】:
已编辑:作为 Java 8,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);
}
}
}
希望这些有用:)
【讨论】:
next() 的合约是抛出一个NoSuchElementException,以防在迭代器没有更多元素的情况下调用该方法,所以我的建议是将if (!hasNext()) throw new NoSuchElementException() 添加为在next() 上要做的第一件事。 :)