Java:相当于 Python 的 range(int, int)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3790142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Equivalent of Python's range(int, int)?
提问by Nick Heiner
Does Java have an equivalent to Python's range(int, int)
method?
Java 是否有等效于 Python 的range(int, int)
方法?
采纳答案by Simon Steele
Guavaalso provides something similar to Python's range
:
Guava还提供了类似于 Python 的东西range
:
Range.closed(1, 5).asSet(DiscreteDomains.integers());
You can also implement a fairly simple iterator to do the same sort of thing using Guava's AbstractIterator:
你也可以使用 Guava 的 AbstractIterator 实现一个相当简单的迭代器来做同样的事情:
return new AbstractIterator<Integer>() {
int next = getStart();
@Override protected Integer computeNext() {
if (isBeyondEnd(next)) {
return endOfData();
}
Integer result = next;
next = next + getStep();
return result;
}
};
回答by Vivien Barousse
public int[] range(int start, int length) {
int[] range = new int[length - start + 1];
for (int i = start; i <= length; i++) {
range[i - start] = i;
}
return range;
}
(Long answer just to say "No")
(长回答只是说“不”)
回答by KeithS
public int[] range(int start, int stop)
{
int[] result = new int[stop-start];
for(int i=0;i<stop-start;i++)
result[i] = start+i;
return result;
}
Forgive any syntax or style errors; I normally program in C#.
原谅任何语法或样式错误;我通常用 C# 编程。
回答by Nikki9696
If you mean to use it like you would in a Python loop, Java loops nicely with the for statement, which renders this structure unnecessary for that purpose.
如果您想像在 Python 循环中那样使用它,Java 可以很好地使用 for 语句循环,这使得该结构对于该目的来说是不必要的。
回答by Amir Rachum
I'm working on a little Java utils library called Jools, and it contains a class Range
which provides the functionality you need (there's a downloadable JAR).
Constructors are either Range(int stop)
, Range(int start, int stop)
, or Range(int start, int stop, int step)
(similiar to a for loop) and you can either iterate through it, which used lazy evaluation, or you can use its toList()
method to explicitly get the range list.
我正在开发一个名为Jools的小型 Java utils 库,它包含一个Range
提供您需要的功能的类(有一个可下载的 JAR)。
构造函数是Range(int stop)
, Range(int start, int stop)
, 或Range(int start, int stop, int step)
(类似于 for 循环),您可以遍历它(使用惰性求值),也可以使用其toList()
方法显式获取范围列表。
for (int i : new Range(10)) {...} // i = 0,1,2,3,4,5,6,7,8,9
for (int i : new Range(4,10)) {...} // i = 4,5,6,7,8,9
for (int i : new Range(0,10,2)) {...} // i = 0,2,4,6,8
Range range = new Range(0,10,2);
range.toList(); // [0,2,4,6,8]
回答by John
Groovy's nifty Range classcan be used from Java, though it's certainly not as groovy.
Groovy 的漂亮Range 类可以从 Java 中使用,尽管它肯定没有那么 groovy。
回答by jiehanzheng
Since Guava 15.0, Range.asSet()has been deprecated and is scheduled to be removed in version 16. Use the following instead:
自 Guava 15.0 起,Range.asSet()已被弃用,并计划在版本 16 中删除。请改用以下内容:
ContiguousSet.create(Range.closed(1, 5), DiscreteDomain.integers());
回答by tkruse
The "Functional Java" library allows to program in such a way to a limited degree, it has a range() method creating an fj.data.Array instance.
“Functional Java”库允许在有限程度上以这种方式编程,它有一个 range() 方法创建一个 fj.data.Array 实例。
See:
看:
Similarly the "Totally Lazy" library offers a lazy range method: http://code.google.com/p/totallylazy/
同样,“Totally Lazy”库提供了一种惰性范围方法:http: //code.google.com/p/totallylazy/
回答by jhodges
Old question, new answer (for Java 8)
旧问题,新答案(适用于 Java 8)
IntStream.range(0, 10).forEach(
n -> {
System.out.println(n);
}
);
or with method references:
或使用方法引用:
IntStream.range(0, 10).forEach(System.out::println);
回答by Julian
You can use the following code snippet in order to get a range set of integers:
您可以使用以下代码片段来获取整数范围集:
Set<Integer> iset = IntStream.rangeClosed(1, 5).boxed().collect
(Collectors.toSet());