remove(int index)

e remove(int index): This e remove int index method in Java uses to remove the element at the specified position from ArrayList. So, It returns the element which after removing the element.

E remove(int index);

Where index, the index of the element to be removed.
E, the element that was removed from the list.
Also, throw, IndexOutOfBoundsException if index is invalid.

import java.util.ArrayList;
public class ArraylistExample 
	public static void main(String[] args) 
		ArrayList<String> listOfNames = new ArrayList<String>();
		// It removes the element at index 1
		String removedElement = listOfNames.remove(1);
		for(String name : listOfNames)
			System.out.println("Names from list = "+name);

Output: GOAL
Names from list = JAVA
Names from list = RAVI

Leave a Comment

Follow us on Instagram & watch the latest videos on YouTube. Click below social icons to visit our Instagram & YouTube profiles.