这是一些可以帮助您入门的东西。您可能要考虑一下,是否希望它在结尾处是包容性的或排他性的,等等。
import org.joda.time.*;import java.util.*;class LocalDateRange implements Iterable<LocalDate>{ private final LocalDate start; private final LocalDate end; public LocalDateRange(LocalDate start, LocalDate end) { this.start = start; this.end = end; } public Iterator<LocalDate> iterator() { return new LocalDateRangeIterator(start, end); } private static class LocalDateRangeIterator implements Iterator<LocalDate> { private LocalDate current; private final LocalDate end; private LocalDateRangeIterator(LocalDate start, LocalDate end) { this.current = start; this.end = end; } public boolean hasNext() { return current != null; } public LocalDate next() { if (current == null) { throw new NoSuchElementException(); } LocalDate ret = current; current = current.plusDays(1); if (current.compareTo(end) > 0) { current = null; } return ret; } public void remove() { throw new UnsupportedOperationException(); } }}class Test{ public static void main(String args[]) { LocalDate start = new LocalDate(2009, 7, 20); LocalDate end = new LocalDate(2009, 8, 3); for (LocalDate date : new LocalDateRange(start, end)) { System.out.println(date); } }}
自从我用Java编写迭代器以来已经有一段时间了,所以我 希望 它是正确的。我认为这还可以…
哦,对于C#迭代器块,这就是我能说的…
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)