Java Program to Insert Element in ArrayList at Specific Position

To insert an element in ArrayList at a specific position, use ArrayList.add(index, element) function where index (= i-1) specifies ith position and the element is the one that is inserted. When the element is inserted, the elements from ith position are shifted right side by a position.

InsertElement.java

</>
Copy
import java.util.ArrayList;

public class InsertElement {

	public static void main(String[] args) {
		ArrayList<String> names = new ArrayList<String>();
		
		names.add("Java");
		names.add("Kotlin");
		names.add("Android");
		
		names.add(2,"Python");
		
		names.forEach(name -> {
			System.out.println(name);
		});
	}
}

Output

Java
Kotlin
Python
Android

Conclusion

In this Java Tutorial, we learned how to insert an element in ArrayList at specific index in Java.