Wednesday, 12 November 2014

Java : Collection Framework : LinkedList (Add & Replace element using ListIterator)


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

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

/*
 * Example of add & set methods 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 add
         * element and replace element.
         */

        while (listIterator.hasNext())
        {

            int indexPosition = listIterator.nextIndex();
            if (indexPosition == 2)
            {
                /*
                 * Inserts the specified element into the list
                 */
                listIterator.add(70000);
            }
            Integer value = listIterator.next();
            if (value == 500)
            {
                /*
                 * Replaces the last element returned by next() or previous()
                 * with the specified element
                 */
                listIterator.set(90000);
            }

        }

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

    }

}

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

linkedList : [100, 200, 70000, 300, 400, 90000]

To Download LinkedListDemoAddSet Project Click the below link

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