set(Object o ) method of ListIterator Interface in Java.

set(Object o ) method of ListIterator Interface in Java.


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

This method do not returns any value. It is an optional operation, this method substitutes those element with a specific element which are lastly backed by the next( )

set(Object o ) method of ListIterator Interface in Java.

In this section we will discuss how can set( ) method be implemented in ListIterator interface in java

Syntax

public void set(Object o )

This method is used to substitute the old element with a specific element into a list.

This method do not returns any value. It is an optional operation, this method substitutes those element with a specific element which are lastly backed by the next( ) or previous( ) method.

Parameter Description

Object o : It takes an element what do you want to replace with old element.

Example of set(Object o) method

In this example we will show you how does set(Object o) method work with ListIterator interface. This example will help you to understand how can you replace the last called element returned by the iterator in forward and backward direction from a list. Through this example we will show you how the set(Object o) method is used after calling next( ) and previous( ) methods for replacing a specified element into list using ListIterator object.

Example:-

package Devmanuals.com;
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
public class ListIteratorSet {
  public static void main(String args[]) {
    List<Integer> llist = new LinkedList<Integer>();
    llist.add(1);
    llist.add(2);
    llist.add(3);
    llist.add(4);
    llist.add(5);
    ListIterator litr = llist.listIterator();
    System.out.println("List in forward order");
    while (litr.hasNext()) {
       System.out.println(litr.next());
    }
    litr.set(6);
    System.out.println("After replacing element in old list then new list is : "+ llist);
  System.out.println("List in reverse order");
    while(litr.hasPrevious()){
    System.out.println(litr.previous());
  }
  litr.set(5);
  System.out.println("After replacing element in old list then new list is : "+ llist);
  }
}

Output :

List in forward order

1

2

3

4

5

After replacing element in old list then new list is : [1, 2, 3, 4, 6]

List in reverse order

6

4

3

2

1

After replacing element in old list then new list is : [5, 2, 3, 4, 6]

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics