Java LinkedList indexOf() Method



Description

The Java LinkedList indexOf(Object) method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. This method is used to search an element within linkedlist and in case of a custom object, that class must implement equals() method for this method to work.

Declaration

Following is the declaration for java.util.LinkedList.indexOf() method

public int indexOf(Object o)

Parameters

o − The element to search for.

Return Value

This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Exception

NA

Checking Index of an Element in a LinkedList of Integers Example

The following example shows the usage of Java LinkedList indexOf(object) method. We're creating a LinkedList of Integers. We're adding couple of Integers to the LinkedList object using add() method calls per element and using indexOf(object) method, we're checking index of an element and printing it.

package com.tutorialspoint;

import java.util.LinkedList;

public class LinkedListDemo {
   public static void main(String[] args) {
      
      // create an empty linked list 
      LinkedList<Integer> linkedList = new LinkedList<>();

      // use add() method to add elements in the linkedList
      linkedList.add(0);
      linkedList.add(1);
      linkedList.add(2);
      linkedList.add(3);
      linkedList.add(4);
      linkedList.add(5);
      linkedList.add(6);
	
      // let us print index of 5
      System.out.println("Index of 5 = " + linkedList.indexOf(5));
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Index of 5 = 5

Checking Index of an Element in a LinkedList of Strings Example

The following example shows the usage of Java LinkedList indexOf(object) method. We're creating a LinkedList of Strings. We're adding couple of Strings to the LinkedList object using add() method calls per element and using indexOf(object) method, we're checking index of an element and printing it.

package com.tutorialspoint;

import java.util.LinkedList;

public class LinkedListDemo {
   public static void main(String[] args) {
      
      // create an empty linked list
      LinkedList<String> linkedList = new LinkedList<>();

      // use add() method to add elements in the linkedList
      linkedList.add("A");
      linkedList.add("B");
      linkedList.add("C");
	  
      // let us print index of B
      System.out.println("Index of B = " + linkedList.indexOf("B"));     
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Index of B = 1

Checking Index of an Element in a LinkedList of Objects Example

The following example shows the usage of Java LinkedList indexOf(object) method. We're creating a LinkedList of Student objects. We're adding couple of Student objects to the LinkedList object using add() method calls per element and using indexOf(object) method, we're checking index of an element and printing it.

package com.tutorialspoint;

import java.util.LinkedList;

public class LinkedListDemo {
   public static void main(String[] args) {

      // create an empty linkedList
      LinkedList<Student> linkedList = new LinkedList<>();

      // use add() method to add elements in the linkedList
      linkedList.add(new Student(1, "Julie"));
      linkedList.add(new Student(2, "Robert"));
      linkedList.add(new Student(3, "Adam"));

      // let us print index of Adam
      System.out.println("Index of Adam = " + linkedList.indexOf(new Student(3, "Adam")));      
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Index of Adam = 2
java_util_linkedlist.htm
Advertisements