lastIndexOf(Object o) method of List Interface in java

lastIndexOf(Object o) method of List Interface in java


Posted in : Core Java Posted on : January 7, 2011 at 6:45 PM Comments : [ 0 ]

In the following example we will show you the method of finding Index of last occurrence of an element in a list using lastIndexOf(Object o) method of List interface in java.

lastIndexOf(Object o) method of List Interface in java

In the following example we will show you the method of finding Index of last occurrence of an element in a list using lastIndexOf(Object o) method of List interface in java.

Syntax

public int lastIndexOf(Object o)

This method returns an index value of last occurrence of an element in a list.

In this method you can find an index value of last occurrence of an element from a list. It returns an integer value of an element in terms of index position of the last occurrence of that specified element or returns (-1) if the specified element does not exist in the list.

Parameter Description

Object o : It takes an argument as an element which is to be searched from list, and returns an integer value of that element as it's index value.

Example of lastIndexOf(Object o) method 

In this example we will show you how does lastIndexOf(Object o) method works in List interface. This example will help you to understand how can you find an index value of last occurrence of an element from the list. Through this example we will show you adding of element to the list, get the element of list with their position value, and index of last occurrence of the specified element. 

Example:-

package Devmanuals.com;
import java.util.ArrayList;
public class ListLastIndexOf {
public static void main(String[] args) {
int in;
ArrayList al = new ArrayList();
al.add("Ram");
al.add("Shyam");
al.add("Krishna");
al.add("Balram");
al.add("Shiv");
al.add("Ram");
System.out.println("Elements of list are :" +al);
System.out.println("size of list = "+al.size());
for(int i=0;i<al.size();i++)
System.out.println("Position of element " +al.get(i) + " is = " +i );
//here method returns Index value
in=al.lastIndexOf("Ram");
System.out.println("Index of last occurrence of Ram = "+in);
//here method returns -1 because Ganesha is not in list
in = al.lastIndexOf("Ganesha");
System.out.println("Index Of element Ganesha = "+in+ " (return (-1) because 'Ganesha' does not exist in above list)");
}
}

Output :

Elements of list are :[Ram, Shyam, Krishna, Balram, Shiv, Ram]

size of list = 6

Position of element Ram is = 0

Position of element Shyam is = 1

Position of element Krishna is = 2

Position of element Balram is = 3

Position of element Shiv is = 4

Position of element Ram is = 5

Index of last occurrence of Ram = 5

Index Of element Ganesha = -1 (return (-1) because 'Ganesha'
does not exist in above list)

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics