Friday, 25 May 2018

Find index of element from ArrayList in java


ArrayList indexOf();

In ArrayList indexOf() is used to find the position of the element.if indexOf return -1 value then element is not found in ArrayList

import java.util.*;

public class Findindex {

    public static void main(String[] args)

{ArrayList al=new ArrayList();

al.add("50");

al.add("100");

al.add("150");

al.add("200");

al.add("250");

int index=al.indexOf("200");

if(index==-1)

{System.out.println("Element is not found in the list");}

else

{System.out.println("Element found at index:"+index);}
    }
   
}

run:
Element found at index:3
BUILD SUCCESSFUL (total time: 0 seconds)

please like and subscribe thank you

No comments:

Post a Comment