Friday 10 October 2014

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
























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

ArrayListIndexOfExample.java
import java.util.ArrayList;

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

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

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

        int index;
        
        index = arrayList.indexOf("Julia");

        System.out.println("Index position of 'Julia' : " + index);
        
        index = arrayList.indexOf("Dave");

        System.out.println("Index position of 'Dave' using indexOf method : " + index);
        
        
        index = arrayList.lastIndexOf("Dave");

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

    }

}

Output
ArrayList : [Ram, Dave, Julia, Dave, Akram, Dave, Steve]

Index position of 'Julia' : 2
Index position of 'Dave' using indexOf method : 1
Index position of 'Dave' using lastIndexOf method : 5

To Download ArrayListDemoIndexOf Project Click the below link

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