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




LIST - FRONT() METHOD


The front() Method is used to get the first element from the List.


Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to get the first element i.e. Mohan from the List.


We can use the front() Method to achieve the above.


Example :



#include <iostream>
#include <list>

using namespace std;

int main() {

    list<string> x = {"Mohan", "Kriti", "Salim"};
    string element = x.front(); 
    cout << element << endl;  
    
    return 0;    
}


Output :



  Mohan

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 front() method to get the first element from the List and store it in a variable element.


string element = x.front();
java_Collections


And when we print the value of element variable, we get Mohan as output.