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

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

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

ListIterator.previousIndex介绍

[英]Returns the index of the element that would be returned by a subsequent call to #previous. (Returns -1 if the list iterator is at the beginning of the list.)
[中]返回后续调用#previous将返回的元素的索引。(如果列表迭代器位于列表的开头,则返回-1。)

代码示例

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

public Object previous() {
  lastReturnedIndex = iter.previousIndex();
  return iter.previous();
}

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

/** An implementation of {@link List#indexOf(Object)}. */
static int indexOfImpl(List<?> list, @Nullable Object element) {
 if (list instanceof RandomAccess) {
  return indexOfRandomAccess(list, element);
 } else {
  ListIterator<?> listIterator = list.listIterator();
  while (listIterator.hasNext()) {
   if (Objects.equal(element, listIterator.next())) {
    return listIterator.previousIndex();
   }
  }
  return -1;
 }
}

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

/** An implementation of {@link List#indexOf(Object)}. */
static int indexOfImpl(List<?> list, @NullableDecl Object element) {
 if (list instanceof RandomAccess) {
  return indexOfRandomAccess(list, element);
 } else {
  ListIterator<?> listIterator = list.listIterator();
  while (listIterator.hasNext()) {
   if (Objects.equal(element, listIterator.next())) {
    return listIterator.previousIndex();
   }
  }
  return -1;
 }
}

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

/**
 * Searches this list for the specified object and returns the index of the
 * first occurrence.
 *
 * @param object
 *            the object to search for.
 * @return the index of the first occurrence of the object, or -1 if it was
 *         not found.
 */
public int indexOf(Object object) {
  ListIterator<?> it = listIterator();
  if (object != null) {
    while (it.hasNext()) {
      if (object.equals(it.next())) {
        return it.previousIndex();
      }
    }
  } else {
    while (it.hasNext()) {
      if (it.next() == null) {
        return it.previousIndex();
      }
    }
  }
  return -1;
}

代码示例来源:origin: EngineHub/WorldEdit

/**
 * Gets the command that was called, which will include the sub-command
 * (i.e. "/br sphere").
 *
 * @param prefix the command shebang character (such as "/") -- may be empty
 * @param spacedSuffix a suffix to put at the end (optional) -- may be null
 * @return the command that was used
 */
public String getCommandUsed(String prefix, @Nullable String spacedSuffix) {
  checkNotNull(prefix);
  StringBuilder builder = new StringBuilder();
  if (prefix != null) {
    builder.append(prefix);
  }
  ListIterator<String> li = commandStack.listIterator(commandStack.size());
  while (li.hasPrevious()) {
    if (li.previousIndex() != commandStack.size() - 1) {
      builder.append(" ");
    }
    builder.append(li.previous());
  }
  if (spacedSuffix != null) {
    if (builder.length() > 0) {
      builder.append(" ");
    }
    builder.append(spacedSuffix);
  }
  return builder.toString().trim();
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testEmptyList() throws Exception {
  ArrayList list = new ArrayList(EMPTY_SET);
  assertEquals(0,list.size());
  assertEquals(true,list.isEmpty());
  assertEquals(false,list.contains(1));
  assertEquals(false,list.iterator().hasNext());
  ListIterator it = list.listIterator();
  assertEquals(false, it.hasNext());
  assertEquals(-1, it.previousIndex());
  assertEquals(0, it.nextIndex());
}
@Test

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

private SimpleClassTable(IntSerializer indexSerializer, List<Class<?>> classes) {
  this.indexSerializer = indexSerializer;
  this.classes = classes;
  ListIterator<Class<?>> iterator = classes.listIterator();
  while (iterator.hasNext()) {
    this.indexes.putIfAbsent(iterator.next(), iterator.previousIndex());
  }
}

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

public Object previous() {
  lastReturnedIndex = iter.previousIndex();
  return iter.previous();
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testEmptyList() throws Exception {
  SetBackedList list = new SetBackedList(EMPTY_SET);
  assertEquals(0,list.size());
  assertEquals(true,list.isEmpty());
  assertEquals(false,list.contains(1));
  assertEquals(false,list.iterator().hasNext());
  ListIterator it = list.listIterator();
  assertEquals(false, it.hasNext());
  assertEquals(-1, it.previousIndex());
  assertEquals(0, it.nextIndex());
}
@Test

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

/**
 *  Traverses to the beginning of the given iterator.
 *
 *  @param iter  the iterator to traverse
 *  @param i     the starting index
 */
private void backwardTest(ListIterator iter, int i) {
  List list = getList();
  while (i > 0) {
    assertTrue("Iterator should have previous, i:" + i, iter.hasPrevious());
    assertEquals("Iterator.nextIndex should work, i:" + i, iter.nextIndex(), i);
    assertEquals("Iterator.previousIndex should work, i:" + i, iter.previousIndex(), i - 1);
    Object o = iter.previous();
    assertEquals("Iterator returned correct element", list.get(i - 1), o);
    i--;
  }
  assertTrue("Iterator shouldn't have previous", !iter.hasPrevious());
  int nextIndex = iter.nextIndex();
  assertEquals("nextIndex should be 0, actual value: " + nextIndex, nextIndex, 0);
  int prevIndex = iter.previousIndex();
  assertEquals("previousIndex should be -1, actual value: " + prevIndex, prevIndex, -1);
  try {
    iter.previous();
    fail("Exhausted iterator should raise NoSuchElement");
  } catch (NoSuchElementException e) {
    // expected
  }
}

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

public static <T extends Comparable<T>> int findMinIndex(final List<T> xs) {
  int minIndex;
  if (xs.isEmpty()) {
    minIndex = -1;
  } else {
    final ListIterator<T> itr = xs.listIterator();
    T min = itr.next(); // first element as the current minimum
    minIndex = itr.previousIndex();
    while (itr.hasNext()) {
      final T curr = itr.next();
      if (curr.compareTo(min) < 0) {
        min = curr;
        minIndex = itr.previousIndex();
      }
    }
  }
  return minIndex;
}

代码示例来源:origin: airbnb/epoxy

public EpoxyModel<?> previous() {
 if (iterator.previousIndex() >= start) {
  return iterator.previous();
 }
 throw new NoSuchElementException();
}

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

private ExternalizerObjectTable(IntSerializer indexSerializer, List<Externalizer<Object>> externalizers) {
  this.indexSerializer = indexSerializer;
  this.externalizers = externalizers;
  ListIterator<Externalizer<Object>> iterator = externalizers.listIterator();
  while (iterator.hasNext()) {
    this.indexes.putIfAbsent(iterator.next().getTargetClass(), iterator.previousIndex());
  }
}

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

public E previous() {
  if (iterator.previousIndex() >= start) {
    return iterator.previous();
  }
  throw new NoSuchElementException();
}

代码示例来源:origin: prestodb/presto

/** An implementation of {@link List#indexOf(Object)}. */
static int indexOfImpl(List<?> list, @NullableDecl Object element) {
 if (list instanceof RandomAccess) {
  return indexOfRandomAccess(list, element);
 } else {
  ListIterator<?> listIterator = list.listIterator();
  while (listIterator.hasNext()) {
   if (Objects.equal(element, listIterator.next())) {
    return listIterator.previousIndex();
   }
  }
  return -1;
 }
}

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

/**
 * Resets this iterator back to the position at which the iterator
 * was created.
 *
 * @since 3.2
 */
@Override
public void reset()  {
  if (iterator instanceof ListIterator) {
    final ListIterator<?> li = (ListIterator<?>) iterator;
    while (li.previousIndex() >= 0) {
      li.previous();
    }
    return;
  }
  currentIndex = 0;
}

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

@Override
public String toString() {
  ListIterator<Token> iter = tokens.listIterator(tokenIterator.previousIndex());
  StringBuilder sb = new StringBuilder();
  if (iter.hasNext()) {
    sb.append(iter.next());
    int count = 1;
    while (iter.hasNext()) {
      if (count > 20) {
        sb.append(" ...");
        break;
      }
      sb.append("  ");
      ++count;
      sb.append(iter.next());
    }
  }
  return sb.toString();
}

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

public Object previous() {
  checkMod();
  lastReturnedIndex = iter.previousIndex();
  return iter.previous();
}

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

/** An implementation of {@link List#indexOf(Object)}. */
static int indexOfImpl(List<?> list, @NullableDecl Object element) {
 if (list instanceof RandomAccess) {
  return indexOfRandomAccess(list, element);
 } else {
  ListIterator<?> listIterator = list.listIterator();
  while (listIterator.hasNext()) {
   if (Objects.equal(element, listIterator.next())) {
    return listIterator.previousIndex();
   }
  }
  return -1;
 }
}

相关文章