java delete arraylist code example
Example 1: How to remove element from arraylist in java
import java.util.ArrayList;
import java.util.List;
public class ArrayListRemoveObjectMethod
{
public static void main(String[] args)
{
List<Integer> al = new ArrayList<>();
al.add(56);
al.add(28);
al.add(39);
al.add(59);
al.add(82);
System.out.println("Before using ArrayList.remove(Object) method size of ArrayList: " + al);
al.remove(new Integer(56));
al.remove(new Integer(28));
System.out.println("After using ArrayList.remove(Object) method size of ArrayList: " + al);
}
}
Example 2: remove item from arraylist in java
List<Integer> al = new ArrayList<>();
al.add(10);
al.add(18);
al.remove(1);
Example 3: ArrayList clear() method in java
import java.util.ArrayList;
public class ArrayListClearMethodExample
{
public static void main(String[] args)
{
ArrayList<Integer> al = new ArrayList<Integer>(4);
al.add(9);
al.add(3);
al.add(5);
al.add(1);
System.out.println("ArrayList before clear() method: " + al);
al.clear();
System.out.println("ArrayList after clear() method: " + al);
}
}