Saturday 1 November 2014

Java : Collection Framework : LinkedList (ListIterator)


Click here to watch in Youtube : https://www.youtube.com/watch?v=GsA6y9xMGy0

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

/*
 * Example of how to use ListIterator
 */
public class LinkedListExample
{

    public static void main( String[] args )
    {
        LinkedList<Integer> linkedList = new LinkedList<Integer>();
        linkedList.add(200);
        linkedList.add(300);
        linkedList.add(10000);
        linkedList.add(5000);
        linkedList.add(2000);

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

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

        /*
         * Using ListIterator move the cursor in forward direction and get each
         * element.
         */

        System.out.println("Forward Direction -----" + "\n");

        while( listIterator.hasNext() )
        {
            int nextIndex = listIterator.nextIndex();
            Integer value = listIterator.next();
            System.out.println(nextIndex + " : " + value);

        }

        System.out.println("\n" + "##############################" + "\n");

        /*
         * Using ListIterator move the cursor in reverse direction and get each
         * element.
         */

        System.out.println("Reverse Direction -----" + "\n");

        while( listIterator.hasPrevious() )
        {
            int previousIndex = listIterator.previousIndex();
            Integer value = listIterator.previous();
            System.out.println(previousIndex + " : " + value);
        }

    }

}

Output
linkedList : [200, 300, 10000, 5000, 2000]

Forward Direction -----

0 : 200
1 : 300
2 : 10000
3 : 5000
4 : 2000

##############################

Reverse Direction -----

4 : 2000
3 : 5000
2 : 10000
1 : 300
0 : 200

To Download LinkedListDemoListIterator Project Click the below link

https://sites.google.com/site/javaee4321/java-collections/LinkedListDemoListIterator.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