ArrayList.add(int index, E element) has the following syntax.
public void add(int index, E element)
In the following code shows how to use ArrayList.add(int index, E element) method.
//from www . ja v a 2 s .c o m import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Integer> arrlist = new ArrayList<Integer> (5); arrlist.add(15); arrlist.add(22); arrlist.add(30); arrlist.add(40); // adding element 25 at third position arrlist.add(2,25); System.out.println(arrlist); } }
The code above generates the following result.