Java Program to Get the First Element From the LinkedList Without Removing It

You are currently viewing Java Program to Get the First Element From the LinkedList Without Removing It

Java program to get the first element from the LinkedList without removing it

// Java program to get the first element from the 
// LinkedList without removing it

import java.util.LinkedList;

public class Main {
  public static void main(String[] args) {
    LinkedList < String > countries = new LinkedList < String > ();

    countries.add("India");
    countries.add("USA");
    countries.add("UK");
    countries.add("CANADA");

    System.out.println("First element: " + countries.element());

    System.out.println("Country names: " + countries);
  }
}

Output

First element: India
Country names: [India, USA, UK, CANADA]

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply