在Java中使用一系列整数获取迭代器的最短路径是什么?换句话说,实现以下内容:
/**
* Returns an Iterator over the integers from first to first+count.
*/
Iterator<Integer> iterator(Integer first, Integer count);
Run Code Online (Sandbox Code Playgroud)
就像是
(first..first+count).iterator()
Run Code Online (Sandbox Code Playgroud)
Sai*_*ali 70
此实现没有内存占用.
/**
* @param begin inclusive
* @param end exclusive
* @return list of integers from begin to end
*/
public static List<Integer> range(final int begin, final int end) {
return new AbstractList<Integer>() {
@Override
public Integer get(int index) {
return begin + index;
}
@Override
public int size() {
return end - begin;
}
};
}
Run Code Online (Sandbox Code Playgroud)
在Java 8中,您可以简单地说:
IntStream.range(begin, end).iterator() // returns PrimitiveIterator.OfInt
Run Code Online (Sandbox Code Playgroud)
或者如果您需要盒装版本:
IntStream.range(begin, end).boxed().iterator() // returns Iterator<Integer>
Run Code Online (Sandbox Code Playgroud)
Joa*_*uer 15
未经测试.将其映射到"min,count"是留给读者的练习.
public class IntRangeIterator implements Iterator<Integer> {
private int nextValue;
private final int max;
public IntRangeIterator(int min, int max) {
if (min > max) {
throw new IllegalArgumentException("min must be <= max");
}
this.nextValue = min;
this.max = max;
}
public boolean hasNext() {
return nextValue <= max;
}
public Integer next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return Integer.valueOf(nextValue++);
}
public void remove() {
throw new UnsupportedOperationException();
}
}
Run Code Online (Sandbox Code Playgroud)
如果你真的想要最短的代码量,那么Bombe的答案就可以了.然而,它没有充分理由吸引记忆.如果你想自己实现它,它将是这样的:
import java.util.*;
public class IntegerRange implements Iterator<Integer>
{
private final int start;
private final int count;
private int position = -1;
public IntegerRange(int start, int count)
{
this.start = start;
this.count = count;
}
public boolean hasNext()
{
return position+1 < count;
}
public Integer next()
{
if (position+1 >= count)
{
throw new NoSuchElementException();
}
position++;
return start + position;
}
public void remove()
{
throw new UnsupportedOperationException();
}
}
Run Code Online (Sandbox Code Playgroud)
使用番石榴框架的一个例子.请注意,这不会实现集合(尽管您必须阅读ContiguousSet实现以验证它).
import com.google.common.collect.ContiguousSet;
import com.google.common.collect.DiscreteDomain;
import com.google.common.collect.DiscreteDomains;
class RangeIterator {
public Iterator<Integer> range(int start, int length) {
assert length > 0;
Range<Integer> dim_range = Ranges.closedOpen(start, start + length);
DiscreteDomain<Integer> ints = DiscreteDomains.integers();
ContiguousSet<Integer> dim = dim_range.asSet(ints);
return dim.iterator();
}
}
Run Code Online (Sandbox Code Playgroud)
在 java 8 中使用流 API 的示例:
int first = 0;
int count = 10;
Iterator<Integer> it = IntStream.range(first, first + count).iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
Run Code Online (Sandbox Code Playgroud)
如果没有迭代器,它可能是:
int first = 0;
int count = 10;
IntStream.range(first, first + count).forEach(i -> System.out.println(i));
Run Code Online (Sandbox Code Playgroud)
直接实施您的作业:
List<Integer> ints = new ArrayList<Integer>();
for (int i = 0; i < count; i++) {
ints.add(first + i);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31871 次 |
| 最近记录: |