Java LinkedList lastIndexOf() Method



Description

The Java LinkedList lastIndexOf(Object) method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

Declaration

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

public int lastIndexOf(Object o)

Parameters

o − The element to search for.

Return Value

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

Exception

NA

Getting Last Index of an Element in a LinkedList of Integers Example

The following example shows the usage of Java LinkedList lastIndexOf(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 lastIndexOf(object) method, we're checking last 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(5);
      linkedList.add(4);
      linkedList.add(5);
      linkedList.add(6);
	
      // let us print last index of 5
      System.out.println("Last index of 5 = " + linkedList.lastIndexOf(5));
   }
}

Output

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

Last index of 5 = 5

Getting Last Index of an Element in a LinkedList of Strings Example

The following example shows the usage of Java LinkedList lastIndexOf(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 lastIndexOf(object) method, we're checking last 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");
      linkedList.add("B");
	  
      // let us print index of B
      System.out.println("Last index of B = " + linkedList.lastIndexOf("B"));     
   }
}

Output

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

Last index of B = 3

Getting Last Index of an Element in a LinkedList of Objects Example

The following example shows the usage of Java LinkedList lastIndexOf(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 lastIndexOf(object) method, we're checking last 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"));
      linkedList.add(new Student(4, "Adam"));

      // let us print last index of Adam
      System.out.println("Last index of Adam = " + linkedList.lastIndexOf(new Student(4, "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 −

Last index of Adam = 3
java_util_linkedlist.htm
Advertisements