Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Remove element at the specified index in ArrayList: remove(int index) example

In the last tutorial I have shared the addAll(int index, Collection c) method example. In this tutorial we will learn about remove(int index) method which is used for removing an Element at the specified index from an ArrayList.You can learn about how ArrayList add(Object) method works internally in java. Syntax :

public Object remove(int index)

remove(int index) example:



import java.util.*;
public class RemoveMethodExample {
public static void main(String args[]) {

//String ArrayList
ArrayListString> al = new ArrayListString>();
al.add("AA");
al.add("BB");
al.add("CC");
al.add("DD");
al.add("AA");
al.add("ZZ");
System.out.println("ArrayList before remove:");
for(String var: al){
System.out.println(var);
}
//Removing 1st element
al.remove(0);
//Removing 3rd element from the remaining list
al.remove(2);
//Removing 4th element from the remaining list
al.remove(2);
System.out.println("ArrayList After remove:");
for(String var: al){
System.out.println(var);
}
}
}


Output

ArrayList before remove:
AA
BB
CC
DD
AA
ZZ
ArrayList After remove:
BB
CC
ZZ



This post first appeared on Java Hungry, please read the originial post: here

Share the post

Remove element at the specified index in ArrayList: remove(int index) example

×

Subscribe to Java Hungry

Get updates delivered right to your inbox!

Thank you for your subscription

×