add(Object o) method of ListIterator Interface in java.

add(Object o) method of ListIterator Interface in java.


Posted in : Core Java Posted on : January 24, 2011 at 7:33 PM Comments : [ 0 ]

This method does not return any value, it only inserts an element just before the next element that would have been returned by the next( ) method and just after the next element that would be returned by the previous( ) method.

add(Object o) method of ListIterator Interface in java.

In this section we will show you how can add(Object o) method be implemented in ListIterator interface.

Syntax

public void add(Object o)

This method is used to add or insert a specified element into a list.

In ListIterator interface add(Object o) method is an optional operation. This method does not return any value, it only inserts an element just before the next element that would have been returned by the next( ) method and just after the next element that would be returned by the previous( ) method. If list does not contain any element, the newly added element is lonesome element on the list. The subsequent call to next method would be unaffected from the position of inserted element, but the previous method will return a new element.

Parameter Description

Object o : It takes an argument what you want to insert into a list.

Example of add(Object o) method

In this example we will show you how does add(Object o) method work in a collection using ListIterator interface. This example will help you to understand how can you insert or add an element into a list. Through this example we will show you how does the listIterator( ) method implement the add(Object o) method and insertion of a specified element into a list at just before the calling of next( ) and just after calling of previous( ) methods using add(Object o) method. 

Example :-

package Devmanuals.com;
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
public class ListIteratorAddElement {
  public static void main(String args[]) {
    List llist = new LinkedList();
    llist.add("C");
    llist.add("C++");
    ListIterator litr = llist.listIterator();
    System.out.println("Output of adding object before next call to next()");
    System.out.println("............................................");
    System.out.println("Old list = " + llist);
    System.out.println("Now cursor is on " + litr.next());
    litr.add(".NET");
    System.out.println("List after adding new element = " + llist + "\n");
    System.out.println("Output of adding object after next call to previous()");
    System.out.println("..............................................");
    System.out.println("Now from list " + llist);
    System.out.println("Now cursor is at = " + litr.previous());
    litr.add("C#");
    System.out.println("List after adding new element = " + llist);
  }
}

Output :-

Output of adding object before next call to next( )

............................................

Old list = [C, C++]

Now cursor is on C

List after adding new element = [C, .NET, C++]

Output of adding object after next call to previous( )

..............................................

Now from list [C, .NET, C++]

Now cursor is at = .NET

List after adding new element = [C, C#, .NET, C++]

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics