本文整理汇总了Java中java.util.ListIterator.forEachRemaining方法的典型用法代码示例。如果您正苦于以下问题:Java ListIterator.forEachRemaining方法的具体用法?Java ListIterator.forEachRemaining怎么用?Java ListIterator.forEachRemaining使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.ListIterator
的用法示例。
在下文中一共展示了ListIterator.forEachRemaining方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testOptimizedForEach
import java.util.ListIterator; //导入方法依赖的package包/类
public void testOptimizedForEach() throws Exception {
final Integer[] data = new Integer[1000 * 1000];
for (int i=0; i < data.length; i++) {
data[i] = i;
}
final List<Integer> source = Arrays.asList(data);
final String[] listClasses = {
"java.util.ArrayList",
"java.util.LinkedList",
"java.util.Vector",
"java.util.concurrent.CopyOnWriteArrayList"
};
final int OFFSET = 3;
final List<Integer> target = new ArrayList<>(source);
for (final String listClass : listClasses) {
final List<Integer> list =
(List<Integer>) Class.forName(listClass).newInstance();
list.addAll(source);
final ListIterator<Integer> iterator = list.listIterator();
assertFalse(iterator.hasPrevious());
for (int i=0; i < OFFSET; i++) {
iterator.next();
}
assertTrue(iterator.hasNext());
assertTrue(iterator.hasPrevious());
assertEquals(iterator.nextIndex(), OFFSET);
assertEquals(iterator.previousIndex(), OFFSET - 1);
iterator.forEachRemaining(e -> {
target.set(e, e + 1);
});
for (int i=OFFSET; i < data.length; i++) {
assertEquals(target.get(i).intValue(), source.get(i)+1);
}
assertFalse(iterator.hasNext());
assertTrue(iterator.hasPrevious());
assertEquals(iterator.nextIndex(), data.length);
assertEquals(iterator.previousIndex(), data.length - 1);
// CopyOnWriteArrayList.listIterator().remove() is unsupported
if (!"java.util.concurrent.CopyOnWriteArrayList".equals(listClass)) {
for (int i = data.length - 1; i >= 0; i--) {
iterator.remove(); // must not throw
if (i > 0) {
iterator.previous();
}
}
assertTrue(list.isEmpty());
}
try {
iterator.next();
fail(listClass + " iterator advanced beyond end");
} catch (NoSuchElementException ignore) {
}
}
}