Saturday, 1 November 2014

Java : Collection Framework : LinkedList (Check the Index Position of the Object)


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

LinkedListIndexOfExample.java
import java.util.LinkedList;

/*
 * Example of indexOf(Object o) and lastIndexOf(Object o) methods
 */
public class LinkedListIndexOfExample
{

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

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

        int index;

        /*
         * Returns the index of the first occurrence of the specified element in
         * this list, or -1 if this list does not contain the element.
         */
        index = linkedList.indexOf(10000);

        System.out.println("Index position of 10000 : " + index);

        index = linkedList.indexOf(300);

        System.out.println("Index position of 300 using indexOf method : "
                + index);

        /*
         * Returns the index of the last occurrence of the specified element in
         * this list, or -1 if this list does not contain the element.
         */
        index = linkedList.lastIndexOf(300);

        System.out.println("Index position of 300 using lastIndexOf method : "
                + index);

    }

}

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

Index position of 10000 : 2
Index position of 300 using indexOf method : 1
Index position of 300 using lastIndexOf method : 3

To Download LinkedListDemoIndex Project Click the below link

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