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>();
		listOfNames.add("JAVA");
		listOfNames.add("GOAL");
		listOfNames.add("RAVI");
				
		// It removes the element at index 1
		String removedElement = listOfNames.remove(1);
		System.out.println(removedElement);
		
		for(String name : listOfNames)
		{
			System.out.println("Names from list = "+name);
		}
	}
}

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

Leave a Comment