java.util.ArrayList remove(int index) Method example

The java.util.ArrayList.remove(int index) is used to remove the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Signature

public E remove(int index) 

This method removes the element of the list at the given index and will throw “java.lang.IndexOutOfBoundsException”  if the index is out of range.

Example

The following example shows the usage of java.util.Arraylist.remove(index) method.

import java.util.ArrayList;

public class RemoveMethodArrayListExample 
{
	public static void main(String args[])
	{
		// create an empty array list
		ArrayList al = new ArrayList();
		
		// use add() method to add elements to the list
		al.add("Element1");
		al.add("Element2");
		al.add("Element3");
		
		//removing element at second position 
		al.remove(1);
		
		//Printing the elements of the list
		System.out.println("Elements of the list");
		for(String temp:al)
		{
			System.out.println(temp);
		}
	}
}

Output

Elements of the list
Element1
Element3

Leave a Reply

Your email address will not be published. Required fields are marked *