remove( ) method of ListIterator interface in java.

remove( ) method of ListIterator interface in java.


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

In this method you can delete an element from the inherent list. This method deletes the last called element of a collection which is backed by the iterator. It is an

remove( ) method of ListIterator interface in java.

In the following example we will show you how can you delete an element from list using remove( ) method of ListIterator interface in java.

Syntax

public void remove( )

This method do not returns any value.

In this method you can delete an element from the inherent list. This method deletes the last called element of a collection which is backed by the iterator. It is an optional operation. This method can be acted only if the add( ) method of ListIterator has not been used after the last call to next or previous.

Parameter Description 

This method does not takes any argument.

Example of remove( ) method 

In this example we will show you how does remove( ) method work with ListIterator interface. This example will help you to understand how can you delete the last called element returned by the iterator from a list. Through this example we will show you how the remove( ) method is used after calling next( ) and previous( ) methods for deleting the element from list using ListIterator object.

Example:-

package Devmanuals.com;
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
public class ListIteratorRemove {
  public static void main(String[] args) {
    LinkedList<Integer> llist = new LinkedList<Integer>();
    for (int i = 10; i <= 20; i++)
      llist.add(i);
    ListIterator<Integer> litr = llist.listIterator();
    System.out.println("Remove the element while travelling in forward direction");
    System.out.println("->->->->->->->");
    System.out.println("Is list contain more elements ? " + litr.hasNext());
    System.out.println("Old list = " + llist);
    System.out.println("Size of old list = "+llist.size());
    while (litr.hasNext()) {
      Integer i1 = (Integerlitr.next();
      if (i1 == 15)
        litr.remove();
    }
    System.out.println("After removing the last called ");
    System.out.println("element by iterator in forward direction, then list =  " + llist);
    System.out.println("Size of list after removing element = "+llist.size());
    System.out.println(" ");
    System.out.println("Remove the element while travelling in backward direction");
    System.out.println("<-<-<-<-<-<-<-");
    System.out.println("Is list contain more elements ? " + litr.hasPrevious());
    System.out.println("Old list = " + llist);
    System.out.println("Size of old list = "+llist.size());
    while(litr.hasPrevious())
  {
    Integer i2 = (Integer)litr.previous();
    if(i2==18)
      litr.remove();
  }
  System.out.println("After removing the last called ");
  System.out.println("element by iterator in backward direction, then list =  " + llist);
  System.out.println("Size of list after removing element = "+llist.size());
  }
}

Output :

Remove the element while travelling in forward direction

->->->->->->->

Is list contain more elements ? true

Old list = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Size of old list = 11

After removing the last called

element by iterator in forward direction, then list = [10, 11, 12, 13, 14, 

16, 17, 18, 19, 20]

Size of list after removing element = 10

Remove the element while travelling in backward direction

<-<-<-<-<-<-<-

Is list contain more elements ? true

Old list = [10, 11, 12, 13, 14, 16, 17, 18, 19, 20]

Size of old list = 10

After removing the last called

element by iterator in backward direction, then list = [10, 11, 12, 13, 14, 

16, 17, 19, 20]

Size of list after removing element = 9

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


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

 
Tutorial Topics