Home Tutorial Java Collections Arraylist Java ArrayList indexof() Method

 
 

Share on Google+Share on Google+
Java ArrayList indexof() Method
Posted on: October 23, 2009 at 12:00 AM
Advertisement
This section demonstrates how to use the indexOf() method in Java Program.

  • It returns the index of the given element present in the ArrayList
  • If there is more than one occurrence of the element then returns the last occurrence.
  • If no element is present then returns the -1.


Example of Java Arraylist indexof() Function
import java.util.*;

public class List1 {

public static void main(String[] args) {
String  ar[]={"india","pakistan","United Kingdom","Japan"};
List list=new ArrayList();
list.add(ar[0]);
list.add(ar[1]);
list.add(ar[2]);
list.add(ar[3]);
System.out.println(list.indexOf("Japan"));
System.out.println(list.indexOf(ar[2]));

}
}
Output
3
2

Advertisement

Related Tags for Java ArrayList indexof() Method:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 23, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com