How to get the element from the specified index of the ArrayList?

How to get the element from the specified index of the ArrayList?

Previous Home Next

 

This example shows how to get /retrieve the element from the specified position of the index.

The get() method of ArraysList is used to get the element from the specified position of the index
 
public E get(int index) Returns the element at the specified position in this list. 

index - index of the element to return,
Returns:
the element at the specified position in this list

 

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);

// TODO Auto-generated method stub

}

}

The ArrayList contains:
10
9
11
12
13
14
15
The element at the specific index position is :10

Previous Home Next