How to know the index position of the specific element in ArrayList?
Previous | Home | Next |
This example shows how to know the index position of the particular element of the ArrayList.
Thepublic int indexOf(Object o) method is used in this example to know the specific index of the particular element.
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.
- Parameters:
o
- element to search for- Returns:
- the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element
package Example;
import java.util.ArrayList;
public class ArrayListExample {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList AList =new ArrayList();//create an arraylist object
AList.add("10");
AList.add("11");
AList.add("12");
AList.add("13");
AList.add("14");
AList.add("15");
System.out.println("The ArrayList contains:");
AList.add(1,"9");
//to display elements of ArrayList
for( int index=0;index<AList.size();index++)
System.out.println(AList.get(index));
Object obj=AList.get(1);
System.out.println("The element at the specific index position is :"+obj);
Object obj1=AList.indexOf("10");
System.out.println("The index of the specific element is:"+obj1);
// TODO Auto-generated method stub
}
}
package Example;
import java.util.ArrayList;
public class ArrayListExample {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList AList =new ArrayList();//create an arraylist object
AList.add("10");
AList.add("11");
AList.add("12");
AList.add("13");
AList.add("14");
AList.add("15");
System.out.println("The ArrayList contains:");
AList.add(1,"9");
//to display elements of ArrayList
for( int index=0;index<AList.size();index++)
System.out.println(AList.get(index));
Object obj=AList.get(1);
System.out.println("The element at the specific index position is :"+obj);
Object obj1=AList.indexOf("10");
System.out.println("The index of the specific element is:"+obj1);
// TODO Auto-generated method stub
}
}
The ArrayList contains:
10
9
11
12
13
14
15
The element at the specific index position is :9
The index of the specific element is:0
Previous | Home | Next |