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

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

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

ListIterator.hasPrevious介绍

[英]Returns true if this list iterator has more elements when traversing the list in the reverse direction. (In other words, returns true if #previous would return an element rather than throwing an exception.)
[中]如果此列表迭代器在反向遍历列表时包含更多元素,则返回true。(换句话说,如果#previous返回元素而不是抛出异常,则返回true。)

代码示例

代码示例来源: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: 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());
}

代码示例来源: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: greenrobot/greenDAO

void appendWhereClause(StringBuilder builder, String tablePrefixOrNull, List<Object> values) {
  ListIterator<WhereCondition> iter = whereConditions.listIterator();
  while (iter.hasNext()) {
    if (iter.hasPrevious()) {
      builder.append(" AND ");
    }
    WhereCondition condition = iter.next();
    condition.appendTo(builder, tablePrefixOrNull);
    condition.appendValuesTo(values);
  }
}

代码示例来源: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) {
  iterator.remove();
  fail("no exception thrown");
 } catch (UnsupportedOperationException expected) {

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

public boolean hasPrevious() {
  // put the cursor in the right position
  if(_indexIterator.previousId() != null && _indexIterator.lastId() != null) {
    if(_indexIterator.previousId().equals(_indexIterator.lastId())) {
      _indexIterator.previous();
    }
  }
  while(!_keyIterator.hasPrevious()) {
    if(_indexIterator.hasPrevious()) {
      _currentIndexId = _indexIterator.previousId();
      List<byte[]> byteList = _indexIterator.previous().getValue();
      _keyIterator = byteList.listIterator(byteList.size());
    } else {
      break;
    }
  }
  return _keyIterator.hasPrevious();
}

代码示例来源: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 {
 } catch (NoSuchElementException expected) {
 iterator.remove();
 assertEquals(asList("2", "3", "4"), list);
 assertFalse(list.isEmpty());

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

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

代码示例来源:origin: groovy/groovy-core

/**
 * Test method for {@link groovy.lang.EmptyRange#listIterator()}.
 */
public void testListIterator() {
  final ListIterator iterator = range.listIterator();
  assertFalse("iterator has next value", iterator.hasNext());
  assertFalse("iterator has previous value", iterator.hasPrevious());
  try {
    iterator.next();
    fail("got next value in an empty range");
  }
  catch (NoSuchElementException e) {
    assertTrue("expected exception thrown", true);
  }
}

代码示例来源:origin: apache/ignite

for (ListIterator<GridCacheMvccCandidate> it = rmts.listIterator(rmts.size()); it.hasPrevious(); ) {
  GridCacheMvccCandidate cur = it.previous();
        mvAfter = new LinkedList<>();
      it.remove();

代码示例来源: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: groovy/groovy-core

/**
 * Test method for {@link groovy.lang.EmptyRange#listIterator(int)}.
 */
public void testListIteratorInt() {
  final ListIterator iterator = range.listIterator(0);
  assertFalse("iterator has next value", iterator.hasNext());
  assertFalse("iterator has previous value", iterator.hasPrevious());
  try {
    range.listIterator(1);
    fail("got list iterator at index 1");
  }
  catch (IndexOutOfBoundsException e) {
    assertTrue("expected exception thrown", true);
  }
}

代码示例来源: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: redisson/redisson

@Override
  public synchronized T get() {
    if(iter.hasNext()) {
      T obj = iter.next().get();
      if(null != obj) {
        return obj;
      }
    } else if(iter.hasPrevious()) {
      // rewind
      while(iter.hasPrevious()) {
        iter.previous();
      }
      return get();
    }
    return null;
  }
};

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

public void testWalkForwardAndBack() {
  ArrayList list = new ArrayList();
  ListIterator it = makeFullListIterator();
  while (it.hasNext()) {
    list.add(it.next());
  assertEquals(false, it.hasNext());
  assertEquals(true, it.hasPrevious());
    assertEquals(obj, it.previous());
  assertEquals(true, it.hasNext());
  assertEquals(false, it.hasPrevious());
  try {
    it.previous();
    fail("NoSuchElementException must be thrown from previous at start of ListIterator");
  } catch (NoSuchElementException e) {

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

@Override public void absorbContent(ListIterator<Content> contentsIter) {
 // This check prevents a repeater from getting added twice.
 // This can happen in the following situation:
 //    RECTANGLE
 //    REPEATER 1
 //    FILL
 //    REPEATER 2
 // In this case, the expected structure would be:
 //     REPEATER 2
 //        REPEATER 1
 //            RECTANGLE
 //        FILL
 // Without this check, REPEATER 1 will try and absorb contents once it is already inside of
 // REPEATER 2.
 if (contentGroup != null) {
  return;
 }
 // Fast forward the iterator until after this content.
 //noinspection StatementWithEmptyBody
 while (contentsIter.hasPrevious() && contentsIter.previous() != this) {}
 List<Content> contents = new ArrayList<>();
 while (contentsIter.hasPrevious()) {
  contents.add(contentsIter.previous());
  contentsIter.remove();
 }
 Collections.reverse(contents);
 contentGroup = new ContentGroup(lottieDrawable, layer, "Repeater", hidden, contents, null);
}

代码示例来源: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: spring-projects/spring-framework

private DefaultResourceTransformerChain initTransformerChain(ResourceResolverChain resolverChain,
    ArrayList<ResourceTransformer> transformers) {
  DefaultResourceTransformerChain chain = new DefaultResourceTransformerChain(resolverChain, null, null);
  ListIterator<? extends ResourceTransformer> it = transformers.listIterator(transformers.size());
  while (it.hasPrevious()) {
    chain = new DefaultResourceTransformerChain(resolverChain, it.previous(), chain);
  }
  return chain;
}

代码示例来源:origin: SonarSource/sonarqube

public void pushForward(List<Token> matchedTokenList) {
 ListIterator<Token> iter = matchedTokenList.listIterator(matchedTokenList.size());
 while (iter.hasPrevious()) {
  tokenQueue.addFirst(iter.previous());
 }
}

相关文章