Wednesday, 12 November 2014

Java : Collection Framework : LinkedList (Remove elements using ListIterator)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=DcpP7iFU2vM&list=UUhwKlOVR041tngjerWxVccw

LinkedListExample.java

import java.util.LinkedList;
import java.util.ListIterator;

/*
 * Example of remove() method of ListIterator
 */
public class LinkedListExample
{

    public static void main(String[] args)
    {
        LinkedList<Integer> linkedList = new LinkedList<Integer>();
        linkedList.add(100);
        linkedList.add(200);
        linkedList.add(300);
        linkedList.add(400);
        linkedList.add(500);

        System.out.println("linkedList : " + linkedList + "\n");

        ListIterator<Integer> listIterator = linkedList.listIterator();

        /*
         * Using ListIterator move the cursor in forward direction and remove
         * element one by one.
         */
        while (listIterator.hasNext())
        {

            listIterator.next();
            /*
             * Removes from the list the last element that was returned by
             * next() or previous() methods
             */
            listIterator.remove();

        }

        System.out.println("linkedList : " + linkedList + "\n");

    }

}

Output

linkedList : [100, 200, 300, 400, 500]

linkedList : []

To Download LinkedListDemoListItrRemove Project Click the below link

https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoListItrRemove.zip?attredirects=0&d=1

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • No comments:

    Post a Comment