Program
import java.util.ArrayList; public class ArrayIndexRemove { public static void main(String[] args) { ArrayList<Integer> a1=new ArrayList<Integer>(); a1.add(12); a1.add(15); a1.add(75); a1.add(45); a1.add(56); int index=2; System.out.println("Initial list" +a1); a1.remove(index); System.out.println("final list" +a1); } }
Output
Initial list[12,15,75,45,56] final list[12,15,45,56]
Description
public E remove(int index)
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
Specified by:
remove in interface List<E>
Overrides:
remove in class AbstractList<E>
Parameters:
index – the index of the element to be removed
Returns:
the element that was removed from the list
Throws:
IndexOutOfBoundsException – if the index is out of range (index < 0 || index >= size())