8:39 pm - Thursday April 27, 2017

Retrieve the first index position of an object int indexOf(Object o)

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

                                        This method returns the index value of the first occurrence of the specified element in the list. This method returns negative value if the specified element is not present in the list.

Example Program

import java.util.ArrayList;

public class FirstIndex 
{
  public static void main(String[] args)

   {

       ArrayList<String> al=new ArrayList<String>();

       System.out.println("Concepts in java:");

       al.add("Constructor");

       al.add("Inheritance");

       al.add("Polymorphism");

       al.add("Exceptional Handling");

       al.add("Inheritance");

       al.add("Casting");

       al.add("Packages");

       System.out.println(al);

       System.out.println("First Occurrence Value");

       int i=al.indexOf("Inheritance");

       System.out.println("The Index value for the inheritance is:" +i);

   }

}

Output

   Concepts in java:

   [Constructor, Inheritance, Polymorphism, Exceptional Handling, Inheritance, Casting, Packages]

   First Occurrence Value

   The Index value for the inheritance is:1

BACK

Filed in: Java Examples

Comments are closed.