Press "Enter" to skip to content

LinkedList lastIndexOf() method with example in java

The lastIndexOf(E element) method of LinkedList returns the last occurrence of the specified element in this linked list.

Syntax

int lastIndexOf(E e)

Parameter

e is the element to find in this linked-list

Return Value

the index of the last occurrence of the specified element

Program

import java.util.LinkedList;

public class LinkedListLastIndexOfExample {

	public static void main(String[] args) {

		LinkedList<Integer> oddNumbers = new LinkedList<>();
		oddNumbers.add(5);
		oddNumbers.add(7);
		oddNumbers.add(9);
		oddNumbers.add(11);
		oddNumbers.add(9);
		oddNumbers.add(15);
		oddNumbers.add(9);
		oddNumbers.add(67);

		System.out.println("Linked List elements: " + oddNumbers);
		System.out.println("Last index of 9: " + oddNumbers.lastIndexOf(9));

	}
}

Output

Linked List elements: [5, 7, 9, 11, 9, 15, 9, 67]
Last index of 9: 6

Be First to Comment

Leave a Reply

Your email address will not be published.