Friday 17 October 2014

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



Click here to watch in Youtube : https://www.youtube.com/watch?v=oEz-vQ-b2RY

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

/*
 * Example of remove() method of 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 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("arrayList : " + arrayList + "\n");

    }

}

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

arrayList : []


To Download ArrayListDemoListIteratorRemove Project Click the below link

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