Java LinkedList IndexOf() Method with Examples

In the previous article, we have discussed about Java LinkedList getLast() Method with Example

In this article we are going to see the use of Java LinkedList IndexOf() method along with suitable examples.

Java LinkedList IndexOf() Method with Examples

This java.util.LinkedList.IndexOf() method is used to get the first index position of the specified element in the LinkedList. It returns the First index position of the specified element (if exist) else returns -1.

Syntax:

LinkedListName.IndexOf()

Where,

  • LinkedListName refers to the name of your LinkedList.

Let’s see different examples to understand it more clearly.

Example-1: Java LinkedList IndexOf() Method – Example with String Type LinkedList

Approach:

  • Create a new LinkedList of type String.
  • Add string elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then find the First index of the specified element using IndexOf() method.
  • Print the index position.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {
        // Create a LinkedList of string datatype
        LinkedList<String> l = new LinkedList<String>();
        // Adding some elements to the LinkedList
        l.add("book");
        l.add("copy");
        l.add("pen");
        l.add("book");
        l.add("pen");
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
       // get the position of specified element occurred first
       System.out.println("First Occurrence of book: " + l.indexOf("book"));
       System.out.println("First Occurrence of notebook: " + l.indexOf("notebook"));
    }
}
Output:

The elements of LinkedList are: [book, copy, pen, book, pen]
First Occurrence of book: 0
First Occurrence of notebook: -1

Example-2: Java LinkedList IndexOf() Method – Example with Integer Type LinkedList

Approach:

  • Create a new LinkedList of type Integer.
  • Add integer elements into the LinkedList using the add() method.
  • Display the LinkedList elements.
  • Then find the First index of the specified element using IndexOf() method.
  • Print the index position.

Program:

import java.util.*;
public class Main
{
    public static void main(String[] args)
    {        
        // Create a LinkedList of Integer datatype
        LinkedList<Integer> l = new LinkedList<Integer>();
        // Adding some elements to the LinkedList
        l.add(2);
        l.add(52);
        l.add(13);
        l.add(17);
        l.add(1);
        l.add(2);
        l.add(52);
        l.add(13);
        l.add(17);
        // Prints the LinkedList elements
        System.out.println("The elements of LinkedList are: "+l);
       // get the position of specified element occurred First
       System.out.println("First Occurrence of 13: " + l.indexOf(13));
       System.out.println("First Occurrence of 99: " + l.indexOf(99));
    }
}
Output:

The elements of LinkedList are: [2, 52, 13, 17, 1, 2, 52, 13, 17]
First Occurrence of 13: 2
First Occurrence of 99: -1

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: