The below example will help you to remove an Arraylist item using the element index. To remove an item from an arrayList I have used Object remove (int index) method. This method returns a reference to the item that was removed. You can see below program it will clearly describe you how to remove item from ArrayList.
public class RemoveItem{
public static void main(String[]args){
ArrayList arrayList = new ArrayList();
arrayList.add("item_1");
arrayList.add("item_2");
arrayList.add("item_3");
arrayList.add("item_4");
//remove an element from the specified index of ArrayList use Object remove(int index)method.
Object obj =arrayList.remove(3);
System.out.print(obj+"is removed frok ArrayList");
System.out.println("Items in ArrayList :");
for(int i=0;i<arrayList.size();i++)
System.out.println(arrayList.get(i));
}
}
0 Comment(s)