Java Program to Get the Index of the First Occurrence of the Specified Item From LinkedList Collection

Java Program to Get the Index of the First Occurrence of the Specified Item From LinkedList Collection

Java Program to Get the Index of the First Occurrence of the Specified Item From LinkedList Collection

// Java program to get the index of the first occurrence of the 
// item from LinkedList collection

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");
    countries.add("USA");

    System.out.println("Country names: " + countries);
    System.out.println("Index of USA: " + countries.indexOf("USA"));
    System.out.println("Index of PAK: " + countries.indexOf("PAK"));
  }
}
Country names: [India, USA, UK, CANADA, USA]
Index of USA: 1
Index of PAK: -1

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

Share on:

Leave a Comment