本文整理匯總了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) {
}
}
}