Skip to content
Related Articles

Related Articles

AbstractList clear() method in Java with Examples
  • Last Updated : 22 Aug, 2019

The clear() method of java.util.AbstractList class is used to remove all of the elements from this list. The list will be empty after this call returns.

Syntax:

public void clear()

Returns Value: This method does not return anything.

Below are the examples to illustrate the clear() method.

Example 1:






// Java program to demonstrate
// clear() method
// for String value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList<String>
            AbstractList<String>
                arrlist1 = new ArrayList<String>();
  
            // Populating arrlist1
            arrlist1.add("A");
            arrlist1.add("B");
            arrlist1.add("C");
            arrlist1.add("D");
            arrlist1.add("E");
  
            // print the ArrayList
            System.out.println("Original ArrayListlist : "
                               + arrlist1);
  
            // Removing all the elements
            // using clear() method
            arrlist1.clear();
  
            // print the new ArrayList
            System.out.println("New ArrayList : "
                               + arrlist1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : "
                               + e);
        }
    }
}
Output:
Original ArrayListlist : [A, B, C, D, E]
New ArrayList : []

Example 2:




// Java program to demonstrate
// clear() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList<Integer>
            AbstractList<Integer>
                arrlist1 = new ArrayList<Integer>();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print the ArrayList
            System.out.println("Original ArrayListlist : "
                               + arrlist1);
  
            // Removing all the elements
            // using clear() method
            arrlist1.clear();
  
            // print the new ArrayList
            System.out.println("New ArrayList : "
                               + arrlist1);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
Output:
Original ArrayListlist : [10, 20, 30, 40, 50]
New ArrayList : []

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :