java.util.ListIterator.previous()方法的使用及代码示例

x33g5p2x  于2022-01-23 转载在 其他  
字(9.3k)|赞(0)|评价(0)|浏览(272)

本文整理了Java中java.util.ListIterator.previous()方法的一些代码示例,展示了ListIterator.previous()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ListIterator.previous()方法的具体详情如下:
包路径:java.util.ListIterator
类名称:ListIterator
方法名:previous

ListIterator.previous介绍

[英]Returns the previous element in the list and moves the cursor position backwards. This method may be called repeatedly to iterate through the list backwards, or intermixed with calls to #next to go back and forth. (Note that alternating calls to next and previous will return the same element repeatedly.)
[中]返回列表中的上一个元素并向后移动光标位置。此方法可以重复调用以向后遍历列表,也可以与对#next的调用混合使用以来回遍历。(请注意,交替调用next和previous将重复返回相同的元素。)

代码示例

代码示例来源:origin: airbnb/lottie-android

@Override public void absorbContent(ListIterator<Content> contents) {
 // Fast forward the iterator until after this content.
 //noinspection StatementWithEmptyBody
 while (contents.hasPrevious() && contents.previous() != this) {}
 while (contents.hasPrevious()) {
  Content content = contents.previous();
  if (content instanceof PathContent) {
   pathContents.add((PathContent) content);
   contents.remove();
  }
 }
}

代码示例来源:origin: google/j2objc

@Override
public T next() {
 if (!hasNext()) {
  throw new NoSuchElementException();
 }
 canRemoveOrSet = true;
 return forwardIterator.previous();
}

代码示例来源:origin: hankcs/HanLP

private static void mergeDate(ListIterator<Vertex> listIterator, Vertex next, Vertex current)
{
  current = Vertex.newTimeInstance(current.realWord + next.realWord);
  listIterator.previous();
  listIterator.previous();
  listIterator.set(current);
  listIterator.next();
  listIterator.next();
  listIterator.remove();
}

代码示例来源:origin: commons-collections/commons-collections

/**
 * Returns the previous object in the list.
 * <p>
 * If at the beginning of the list, return the last element. Note
 * that in this case, traversal to find that element takes linear time.
 *
 * @return the object before the last element returned
 * @throws NoSuchElementException if there are no elements in the list
 */
public Object previous() {
  if (list.isEmpty()) {
    throw new NoSuchElementException(
      "There are no elements for this iterator to loop on");
  }
  if (iterator.hasPrevious() == false) {
    Object result = null;
    while (iterator.hasNext()) {
      result = iterator.next();
    }
    iterator.previous();
    return result;
  } else {
    return iterator.previous();
  }
}

代码示例来源:origin: spring-projects/spring-framework

private static DefaultWebFilterChain initChain(List<WebFilter> filters, WebHandler handler) {
  DefaultWebFilterChain chain = new DefaultWebFilterChain(filters, handler, null, null);
  ListIterator<? extends WebFilter> iterator = filters.listIterator(filters.size());
  while (iterator.hasPrevious()) {
    chain = new DefaultWebFilterChain(filters, handler, iterator.previous(), chain);
  }
  return chain;
}

代码示例来源:origin: google/guava

public void testEmptyListIterator() {
 ListIterator<String> iterator = Iterators.emptyListIterator();
 assertFalse(iterator.hasNext());
 assertFalse(iterator.hasPrevious());
 assertEquals(0, iterator.nextIndex());
 assertEquals(-1, iterator.previousIndex());
 try {
  iterator.next();
  fail("no exception thrown");
 } catch (NoSuchElementException expected) {
  iterator.previous();
  fail("no exception thrown");
 } catch (NoSuchElementException expected) {

代码示例来源:origin: ReactiveX/RxJava

assertEquals(0, list.size());
assertFalse(list.contains(1));
assertFalse(list.remove((Integer)1));
assertEquals(7, list.size());
assertEquals(7, list.size());
ListIterator<Integer> lit = list.listIterator(7);
for (int i = 7; i > 0; i--) {
  assertEquals(i, lit.previous().intValue());

代码示例来源:origin: jphp-group/jphp

@Override
public boolean hasNext() {
  int offset = 0;
  while (iterator.hasNext()){
    offset++;
    if (iterator.next() == null){
    } else
      break;
  }
  for(int i = 0; i < offset; i++)
    iterator.previous();
  return offset > 0;
}

代码示例来源:origin: commons-collections/commons-collections

public void testBugCollections447() {
    final List treeList = new TreeList();
    treeList.add("A");
    treeList.add("B");
    treeList.add("C");
    treeList.add("D");
    final ListIterator li = treeList.listIterator();
    assertEquals("A", li.next());
    assertEquals("B", li.next());
    assertEquals("B", li.previous());
    li.remove(); // Deletes "B"
    // previous() after remove() should move to
    // the element before the one just removed
    assertEquals("A", li.previous());
  }    
}

代码示例来源:origin: google/guava

private static void assertTransformListIterator(List<String> list) {
 ListIterator<String> iterator = list.listIterator(1);
 assertEquals(1, iterator.nextIndex());
 assertEquals("2", iterator.next());
 assertEquals("3", iterator.next());
 assertEquals("4", iterator.next());
 assertEquals(4, iterator.nextIndex());
 try {
 assertEquals("4", iterator.previous());
 assertEquals("3", iterator.previous());
 assertEquals("2", iterator.previous());
 assertTrue(iterator.hasPrevious());
 assertEquals("1", iterator.previous());
 assertFalse(iterator.hasPrevious());
 assertEquals(-1, iterator.previousIndex());
 try {
  iterator.previous();
  fail("did not detect beginning of list");
 } catch (NoSuchElementException expected) {

代码示例来源:origin: jphp-group/jphp

protected void checkUnexpectedStart(ListIterator<Token> iterator){
  if (!iterator.hasPrevious()){
    iterator.next();
    Token current = iterator.previous();
    unexpectedToken(current);
  }
}

代码示例来源:origin: commons-collections/commons-collections

private void walkBackward(ListIterator expected, ListIterator testing) {
  while(expected.hasPrevious()) {
    assertEquals(expected.nextIndex(),testing.nextIndex());
    assertEquals(expected.previousIndex(),testing.previousIndex());
    assertTrue(testing.hasPrevious());
    assertEquals(expected.previous(),testing.previous());
  }
}

代码示例来源:origin: google/j2objc

/** An implementation of {@link List#lastIndexOf(Object)}. */
static int lastIndexOfImpl(List<?> list, @NullableDecl Object element) {
 if (list instanceof RandomAccess) {
  return lastIndexOfRandomAccess(list, element);
 } else {
  ListIterator<?> listIterator = list.listIterator(list.size());
  while (listIterator.hasPrevious()) {
   if (Objects.equal(element, listIterator.previous())) {
    return listIterator.nextIndex();
   }
  }
  return -1;
 }
}

代码示例来源:origin: wildfly/wildfly

/**
 * Returns the previous object in the list.
 * <p>
 * If at the beginning of the list, return the last element. Note
 * that in this case, traversal to find that element takes linear time.
 *
 * @return the object before the last element returned
 * @throws NoSuchElementException if there are no elements in the list
 */
public Object previous() {
  if (list.isEmpty()) {
    throw new NoSuchElementException(
      "There are no elements for this iterator to loop on");
  }
  if (iterator.hasPrevious() == false) {
    Object result = null;
    while (iterator.hasNext()) {
      result = iterator.next();
    }
    iterator.previous();
    return result;
  } else {
    return iterator.previous();
  }
}

代码示例来源:origin: google/guava

/** An implementation of {@link List#lastIndexOf(Object)}. */
static int lastIndexOfImpl(List<?> list, @Nullable Object element) {
 if (list instanceof RandomAccess) {
  return lastIndexOfRandomAccess(list, element);
 } else {
  ListIterator<?> listIterator = list.listIterator(list.size());
  while (listIterator.hasPrevious()) {
   if (Objects.equal(element, listIterator.previous())) {
    return listIterator.nextIndex();
   }
  }
  return -1;
 }
}

代码示例来源:origin: commons-collections/commons-collections

private void walkLists(List list, ListIterator testing) {
  ListIterator expected = list.listIterator();
  while(expected.hasNext()) {
    assertEquals(expected.nextIndex(),testing.nextIndex());
    assertEquals(expected.previousIndex(),testing.previousIndex());
    assertTrue(testing.hasNext());
    assertEquals(expected.next(),testing.next());
    assertTrue(testing.hasPrevious());
    assertEquals(expected.previous(),testing.previous());
    assertTrue(testing.hasNext());
    assertEquals(expected.next(),testing.next());
  for(int i=0;i<list.size();i++) {
      assertEquals(expected.nextIndex(),testing.nextIndex());
      assertEquals(expected.previousIndex(),testing.previousIndex());
      assertTrue(expected.hasPrevious()); // if this one fails we've got a logic error in the test
      assertTrue(testing.hasPrevious());
      assertEquals(expected.previous(),testing.previous());
      assertTrue(expected.hasPrevious()); // if this one fails we've got a logic error in the test
      assertTrue(testing.hasPrevious());
      assertEquals(expected.previous(),testing.previous());
      if(expected.hasPrevious()) {
        assertEquals(walkdescr.toString(),expected.previous(),testing.previous());

代码示例来源:origin: org.apache.commons/commons-lang3

int getMaxWidth(final ListIterator<StrategyAndWidth> lt) {
    if(!strategy.isNumber() || !lt.hasNext()) {
      return 0;
    }
    final Strategy nextStrategy = lt.next().strategy;
    lt.previous();
    return nextStrategy.isNumber() ?width :0;
  }
}

代码示例来源:origin: jankotek/mapdb

NoSuchElementException.class,
() -> x.iterator().next(),
() -> x.listIterator().next(),
() -> x.listIterator(0).next(),
() -> x.listIterator().previous(),
() -> x.listIterator(0).previous());

代码示例来源:origin: commons-collections/commons-collections

private void nextNextPrevious(ListIterator expected, ListIterator testing) {
  // calls to next() should change the value returned by previous()
  // even after previous() has been set by a call to hasPrevious()
  assertEquals(expected.next(),testing.next());
  assertEquals(expected.hasPrevious(),testing.hasPrevious());
  Object expecteda = expected.next();
  Object testinga = testing.next();
  assertEquals(expecteda,testinga);
  Object expectedb = expected.previous();
  Object testingb = testing.previous();
  assertEquals(expecteda,expectedb);
  assertEquals(testinga,testingb);
}

代码示例来源:origin: stackoverflow.com

// Substitute appropriate type.
ArrayList<...> a = new ArrayList<...>();

// Add elements to list.

// Generate an iterator. Start just after the last element.
ListIterator li = a.listIterator(a.size());

// Iterate in reverse.
while(li.hasPrevious()) {
 System.out.println(li.previous());
}

相关文章