Friday 10 October 2014

Java : Collection Framework : ArrayList (ListIterator)
























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

ArrayListExample.java
import java.util.ArrayList;
import java.util.ListIterator;

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

    public static void main( String[] args )
    {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("Ram");
        arrayList.add("Dave");
        arrayList.add("Peter");
        arrayList.add("Julia");
        arrayList.add("Akram");

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

        ListIterator<String> listIterator = arrayList.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();
            String name = listIterator.next();
            System.out.println(nextIndex + " : " + name);

        }

        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();
            String name = listIterator.previous();
            System.out.println(previousIndex + " : " + name);
        }

    }

}

Output
arrayList : [Ram, Dave, Peter, Julia, Akram]

Forward Direction -----

0 : Ram
1 : Dave
2 : Peter
3 : Julia
4 : Akram

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

Reverse Direction -----

4 : Akram
3 : Julia
2 : Peter
1 : Dave
0 : Ram

To Download ArrayListDemoListItrerator Project Click the below link

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