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

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

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

ListIterator.add介绍

[英]Inserts the specified element into the list (optional operation). The element is inserted immediately before the element that would be returned by #next, if any, and after the element that would be returned by #previous, if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element. (This call increases by one the value that would be returned by a call to nextIndex or previousIndex.)
[中]将指定的元素插入列表(可选操作)。元素被插入到将由#next(如果有)返回的元素之前,以及将由#previous(如果有)返回的元素之后。(如果列表中不包含任何元素,则新元素将成为列表中的唯一元素。)新元素插入到隐式游标之前:对next的后续调用将不受影响,对previous的后续调用将返回新元素。(此调用将调用nextIndex或previousIndex返回的值增加一倍。)

代码示例

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

/** An implementation of {@link List#addAll(int, Collection)}. */
static <E> boolean addAllImpl(List<E> list, int index, Iterable<? extends E> elements) {
 boolean changed = false;
 ListIterator<E> listIterator = list.listIterator(index);
 for (E e : elements) {
  listIterator.add(e);
  changed = true;
 }
 return changed;
}

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

@Override
public void add(T e) {
 forwardIterator.add(e);
 forwardIterator.previous();
 canRemoveOrSet = false;
}

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

public static List<IWord> spilt(List<IWord> wordList)
  {
    ListIterator<IWord> listIterator = wordList.listIterator();
    while (listIterator.hasNext())
    {
      IWord word = listIterator.next();
      if (word instanceof CompoundWord)
      {
        listIterator.remove();
        for (Word inner : ((CompoundWord) word).innerList)
        {
          listIterator.add(inner);
        }
      }
    }
    return wordList;
  }
}

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

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

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

ListIterator<Vertex> listIterator = vertexList.listIterator();
listIterator.next();
int length = vertexList.size() - 2;
for (int i = 0; i < length; ++i)
  Vertex vertex = listIterator.next();
  Term termMain = convert(vertex);
  termList.add(termMain);
          listIterator.add(smallVertex);
          Term termSub = convert(smallVertex);
          termSub.offset = currentLine - 1;

代码示例来源:origin: Sable/soot

public boolean add(Object toadd) {
 if (contains(toadd)) {
  return false;
 }
 /* it is not added to the end, but keep it in the order */
 int index = fulllist.indexOf(toadd);
 for (ListIterator worklistIter = worklist.listIterator(); worklistIter.hasNext();) {
  Object tocomp = worklistIter.next();
  int tmpidx = fulllist.indexOf(tocomp);
  if (index < tmpidx) {
   worklistIter.add(toadd);
   return true;
  }
 }
 return false;
}

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

SampleItem item = it.next();
for (int i = start; i < bufferCount; i++) {
 long v = buffer[i];
 while (it.nextIndex() < samples.size() && item.value < v) {
  item = it.next();
  it.previous();
 it.add(newItem);
 item = newItem;

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

/**
 * Test remove after add behaviour.
 */
public void testAddThenRemove() {
  ListIterator it = makeFullListIterator();
  
  // add then remove
  if (supportsAdd() && supportsRemove()) {
    it.next();
    it.add(addSetValue());
    try {
      it.remove();
      fail("IllegalStateException must be thrown from remove after add");
    } catch (IllegalStateException e) {
    }
  }
}

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

GridCacheMvccCandidate c = it.previous();
  it.next();
  it.add(cand);
  it.next();
  it.add(cand);

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

GridCacheMvccCandidate cur = it.previous();
      mvAfter = new LinkedList<>();
    it.remove();
  it.add(cand);

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

public void testAddThenSet() {
  ListIterator it = makeFullListIterator();        
  // add then set
  if (supportsAdd() && supportsSet()) {
    it.next();
    it.add(addSetValue());
    try {
      it.set(addSetValue());
      fail("IllegalStateException must be thrown from set after add");
    } catch (IllegalStateException e) {
    }
  }
}

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

/**
 * Replaces the void `<init>(boolean)` constructor for a call to the
 * `void <init>(int, int, int)` one.
 */
private void replaceGregorianCalendarBooleanConstructor(ListIterator<AbstractInsnNode> instructions, MethodInsnNode targetMethod) {
 // Remove the call to GregorianCalendar(boolean)
 instructions.remove();
 // Discard the already-pushed parameter for GregorianCalendar(boolean)
 instructions.add(new InsnNode(Opcodes.POP));
 // Add parameters values for calling GregorianCalendar(int, int, int)
 instructions.add(new InsnNode(Opcodes.ICONST_0));
 instructions.add(new InsnNode(Opcodes.ICONST_0));
 instructions.add(new InsnNode(Opcodes.ICONST_0));
 // Call GregorianCalendar(int, int, int)
 instructions.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, targetMethod.owner, targetMethod.name, "(III)V", targetMethod.itf));
}

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

@Override
  public List<IWord> flow(List<IWord> input)
  {
    ListIterator<IWord> listIterator = input.listIterator();
    while (listIterator.hasNext())
    {
      IWord wordOrSentence = listIterator.next();
      if (wordOrSentence.getLabel() != null)
        continue; // 这是别的管道已经处理过的单词,跳过
      listIterator.remove(); // 否则是句子
      String sentence = wordOrSentence.getValue();
      Matcher matcher = pattern.matcher(sentence);
      int begin = 0;
      int end;
      while (matcher.find())
      {
        end = matcher.start();
        listIterator.add(new Word(sentence.substring(begin, end), null)); // 未拦截的部分
        listIterator.add(new Word(matcher.group(), label)); // 拦截到的部分
        begin = matcher.end();
      }
      if (begin < sentence.length()) listIterator.add(new Word(sentence.substring(begin), null));
    }
    return input;
  }
}

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

if (!it.hasNext()) {
  return phoneList;
    it.previous(); // one step back
    it.add("-"); // insert syllable boundary
    it.next(); // and forward again
int minIndex = -1; // position of the sonority minimum
int syllableStart = -1;
while (it.hasNext()) {
        while (it.nextIndex() > minIndex) {
          steps++;
          it.previous();
        it.add(".");
    it.previous(); // skip . backwards
          it.remove(); // remove the .
          it.next(); // skip one ph
          it.add("-");
        } else {
        it.remove(); // remove _
        it.next(); // skip N forwards
        it.add("-"); // insert -

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

public void testListIteratorAdd() {
  // override to cope with Set behaviour
  resetEmpty();
  List list1 = getList();
  List list2 = getConfirmedList();
  Object[] elements = getOtherElements();  // changed here
  ListIterator iter1 = list1.listIterator();
  ListIterator iter2 = list2.listIterator();
  for (int i = 0; i < elements.length; i++) {
    iter1.add(elements[i]);
    iter2.add(elements[i]);
    super.verify();  // changed here
  }
  resetFull();
  iter1 = getList().listIterator();
  iter2 = getConfirmedList().listIterator();
  for (int i = 0; i < elements.length; i++) {
    iter1.next();
    iter2.next();
    iter1.add(elements[i]);
    iter2.add(elements[i]);
    super.verify();  // changed here
  }
}

代码示例来源:origin: Sable/soot

private void push(int numstate, ArrayList<Object> listNode, boolean hidden) throws ParserException, LexerException, IOException
{
  this.nodeList = listNode;
  if(!hidden)
  {
    filter();
  }
  if(!this.stack.hasNext())
  {
    this.stack.add(new State(numstate, this.nodeList));
    return;
  }
  State s = (State) this.stack.next();
  s.state = numstate;
  s.nodes = this.nodeList;
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

SampleItem item = it.next();
for (int i = start; i < bufferCount; i++) {
 long v = buffer[i];
 while (it.nextIndex() < samples.size() && item.value < v) {
  item = it.next();
  it.previous();
 it.add(newItem);
 item = newItem;

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

listIterator.remove();
  listIterator.add("a");
listIterator.next();
  listIterator.remove();
  listIterator.add("a");

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

/** An implementation of {@link List#addAll(int, Collection)}. */
static <E> boolean addAllImpl(List<E> list, int index, Iterable<? extends E> elements) {
 boolean changed = false;
 ListIterator<E> listIterator = list.listIterator(index);
 for (E e : elements) {
  listIterator.add(e);
  changed = true;
 }
 return changed;
}

相关文章