How to add an element at the specified position of an ArrayList?

How to add an element at the specified position of an ArrayList?

Previous Home Next

 

This program will  shows how add an element at the specified index of an ArrayList .

TO add an element at the specified position of the index OF ArrayList  use void add(int indx ,object obj) metho
 add () methos in ArrayList  does not override the element it will shift the previous value to the right and add the new value at specific position.


 

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);
AList.add(1,"9");
//to display elements of ArrayList
for( int index=0;index<AList.size();index++)
System.out.println(AList.get(index));

// TODO Auto-generated method stub

}

}

The ArrayList contains:
10
9
11
12
13
14
15

Previous Home Next