LinkedList lastIndexOf() method with example in java

Published by user on

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 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
Categories: Java

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *