Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




LIST - CLEAR() METHOD


The clear() Method is used to remove all the elements from the List.


Example :



#include <iostream>
#include <list>

using namespace std;

int main() {

    list<string> x = {"Mohan", "Kriti", "Salim"};
        
    x.clear();
    for (string data : x)  
    {  
        cout << data << endl;  
    }
    
    return 0;    
}


Output :




So, in the above code we have created a List and initialised to the variable x.


list<string> x = {"Mohan", "Kriti", "Salim"};

Below is how the values are positioned in the List,

java_Collections

Next, we have used the clear() method that removes all the elements from the List making the List empty.


x.clear();

And we get an empty List as output,