Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7182407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:45:52+00:00 2026-05-28T17:45:52+00:00

The lastIndexOf() method of List interface accepts a parameter which is of type Object.

  • 0

The lastIndexOf() method of List interface accepts a parameter which is of type Object.

However, the add() method accepts a parameter of type E (which is the generic-type of the List which is defined at the time of creating a List)
Since add() accepts only E, this prevents the developer (or user) to add any incompatible object to the list at compile-time itself.

Now, Java doc says that lastIndexOf() can throw ClassCastException if the object passed is incompatible. However, when I run the following code in Eclipse Helios I don’t get any Exception :-

package scjp.collection.list;

import java.util.ArrayList;
import java.util.List;

public class LastIndexOf {
public static void main(String[] args) {
    List<String> list = new ArrayList<String>();
    
    list.add("some");
    list.add("thing");
    list.add("at");
    list.add("last");
    list.add("and");
    list.add("at");
    list.add("again");
    
    
    
    System.out.println(list.lastIndexOf("at"));
    System.out.println(list.lastIndexOf(10));                    // # 1
    System.out.println(list.lastIndexOf(new LastIndexOf()));     // # 2
}
}

At Line 1 and Line 2, I have passed incompatible objects to my List which is of type String.
However, the output that I get is :-

5
-1
-1

I get no ClassCastException.

Had the lastIndexOf() method been accepting objects of type E rather than objects of type Object, this would have been prevented at compile-time only. Why is this not done??

Java creators must have thought some problem that could occur if it accept E (and not Object). What harm would it be??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:45:53+00:00Added an answer on May 28, 2026 at 5:45 pm

    Here’s an example that might help. Note that the equals method used by ArrayList<T> will return true if passed a LinkedList<T> with equal elements in the same order. So, if you have a List<ArrayList<Integer>>, it would be valid to call lastIndexOf on it, passing a LinkedList<Integer>, in order to find an equal ArrayList<Integer>. But if lastIndexOf were defined in the way you describe, this wouldn’t be possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What are the reasons why Map.get(Object key) is not (fully) generic This
I have a MVC action method from which I am returning the data as
I need to add two polynomials together using a recursive method. This is for
I want to return multiple parameter from a method in c#.I just wanted to
I have strings which look like string1 = ~01301~^~DATA1,DATA2 DATA3~^15.87^717^0.85^81.11^2.11^0.06^0 string2 = ~01341~^~DATA3,DATA2 DATA1
I have an extension method below, but when I run this, the foreach gives
When I click in field, type text, and press return on keyboard it triggers
I'm trying to get a list of the files on an FTP server, then
one problem I'm having is i have 2 JTextAreas and i need to add
I am having problems trying to use the file.Close with StreamWriter in this method,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.