ListIterator add - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for ListIterator add method.

This code example shows how to use the following methods:add, next, previous, remove
    public static 
    void main(String[] args) {
        LinkedList list = new LinkedList();
        ListIterator e = list.listIterator();
        Object o = new Integer(1);
        e.add(o);
        e.previous();
        e.next();
        e.remove();
        e.add(o);
        if (!o.equals(list.get(0)))
            throw new RuntimeException("LinkedList ListIterator remove failed.");
    } 
} 
7
CodeRank
This code example shows how to use the following methods:add, hasNext, next, previous, remove
		Object c = new Object();
 
		dynamicList.add(a);
 
		try { 
			iter.set(c);
			fail("should have thrown " + IllegalStateException.class);
		} 
		catch (IllegalStateException ex) {
			// expected 
		} 
 
		iter.next();
		iter.previous();
		iter.set(b);
 
		assertEquals(1, dynamicList.size());
		assertSame(b, dynamicList.get(0));
		assertTrue(iter.hasNext());
		// move forward and back 

6
CodeRank
This code example shows how to use the following methods:add, hasNext, next, previous
  } 
   
  public void testAdd() { 
    ListIterator<String> iterator = create();
 
    assertTrue(iterator.hasNext());
    assertEquals("a", iterator.next());
    assertEquals("b", iterator.next());
    assertEquals("b", iterator.previous());
    try { 
      iterator.add("c");
      fail();
    } catch (UnsupportedOperationException expected) {}
  } 
   
  public void testSet() { 
    ListIterator<String> iterator = create();
 
    assertTrue(iterator.hasNext());
    assertEquals("a", iterator.next());
6
CodeRank
This code example shows how to use the following methods:add, next, previous, remove
    public static 
    void main(String[] args) {
        LinkedList list = new LinkedList();
        ListIterator e = list.listIterator();
        Object o = new Integer(1);
        e.add(o);
        e.previous();
        e.next();
        e.remove();
        e.add(o);
        if (!o.equals(list.get(0)))
            throw new RuntimeException("LinkedList ListIterator remove failed.");
    } 
} 
6
CodeRank
This code example shows how to use the following methods:add, next, previous, remove
    public static 
    void main(String[] args) {
        LinkedList list = new LinkedList();
        ListIterator e = list.listIterator();
        Object o = new Integer(1);
        e.add(o);
        e.previous();
        e.next();
        e.remove();
        e.add(o);
        if (!o.equals(list.get(0)))
            throw new RuntimeException("LinkedList ListIterator remove failed.");
    } 
} 
7
CodeRank
This code example shows how to use the following methods:add, hasNext, next, previous
				previous(); 
			} else { 
				nxt = null;
			} 
			onAdd(o, nxt);
			_iter.add(o);
		} 
		 
		public boolean hasNext() { 
			return _iter.hasNext();
		} 
		 
		public boolean hasPrevious() { 
			return _iter.hasPrevious();
		} 
		 
		public E next() { 
			_last = _iter.next();
			_lastReady = true;
			return _last;

6
CodeRank
This code example shows how to use the following methods:add, next, previous, remove
    public static 
    void main(String[] args) {
        LinkedList list = new LinkedList();
        ListIterator e = list.listIterator();
        Object o = new Integer(1);
        e.add(o);
        e.previous();
        e.next();
        e.remove();
        e.add(o);
        if (!o.equals(list.get(0)))
            throw new RuntimeException("LinkedList ListIterator remove failed.");
    } 
} 
6
CodeRank
This code example shows how to use the following methods:add, hasNext, next, previous
			return; 
		} 
 
		ListIterator<SimulationEvent> iterator = list.listIterator(); 
		SimulationEvent event; 
		while (iterator.hasNext()) { 
			event = iterator.next(); 
			if (event.getEventTime() > eventTime) { 
				iterator.previous(); 
				iterator.add(newEvent); 
				return; 
			} 
		} 
 
		list.add(newEvent); 
	} 
 
	/** 
	 * Returns an iterator to the events in the queue. 
	 *  
6
CodeRank
This code example shows how to use the following methods:add, hasNext, hasPrevious, next, nextIndex
        testEmptyListIterator(Collections.emptyList().listIterator(0));
    } 
 
    private void testEmptyListIterator(ListIterator<?> i) {
        assertFalse(i instanceof Serializable);
        assertFalse(i.hasNext());
        assertFalse(i.hasPrevious());
        assertEquals(0, i.nextIndex());
        try { 
            i.next();
            fail();
        } catch (NoSuchElementException expected) {
        } 
        assertEquals(-1, i.previousIndex());
        try { 
            i.previous();
            fail();
        } catch (NoSuchElementException expected) {
        } 
        try { 

6
CodeRank
This code example shows how to use the following methods:add, next, remove
      staff.addLast("Tom");
       
      // | in the comments indicates the iterator position 
 
      ListIterator<String> iterator = staff.listIterator(); // |DHRT
      iterator.next(); // D|HRT
      iterator.next(); // DH|RT
      System.out.println(staff);
 
      // Add more elements after second element 
       
      iterator.add("Juliet"); // DHJ|RT
      iterator.add("Nina"); // DHJN|RT
 
      iterator.next(); // DHJNR|T
 
      // Remove last traversed element  
 
      iterator.remove(); // DHJN|T
      

6
CodeRank
add Method Overview
Inserts the specified element into the list (optional operation).
See Code Examples for other Java 8 ListIterator Methods: