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

  • SEARCH
  • Home
  • 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 8688911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:30:03+00:00 2026-06-12T23:30:03+00:00

This is a sample implementation of indexOf() in LinkedList on Oracle’s website. I’m a

  • 0

This is a sample implementation of indexOf() in LinkedList on Oracle’s website. I’m a little confused on how the if loop works here:

public int indexOf(E e) {
    for (ListIterator<E> it = listIterator(); it.hasNext(); )
        if (e == null ? it.next() == null : e.equals(it.next()))
            return it.previousIndex();
    // Element not found
    return -1;
}

So the ListIterator object is created at the head of the list. for loop goes on until the iterator reaches the list’s end, and the if loop checks if the target object is found. The part I did not understand though, is that why does the if loop check it.next() == null when e == null? Could someone help walk me through how it’s done when input e is null?

  • 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-06-12T23:30:06+00:00Added an answer on June 12, 2026 at 11:30 pm

    The loop checks if it.next() == null only if e == null this is done to avoid NullPointerException when evaluating e.equals(it.next()).

    if e != null, then the regular e.equals() method is invoked.

    null is a valid “element” that can be inserted to a LinkedList, so this must be taken into consideration.

    The position of the last element is not inserted. Note that unlike the text book data structure where the last element in a Linked List is null, in here – when you get to the last element – it.hasNext() will be evaluated to false, without letting you see this “junk” element.

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

Sidebar

Related Questions

in this sample method/message: -(void) setNumerator:(int) n { numerator = n; } What does
Take this sample class as an example: [AttributeUsage(AttributeTargets.All, AllowMultiple=true)] public class BugFixAttribute : System.Attribute
Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
I ask similar question here I use this sample to implement namedpipes in win
I was writing an answer to this question when noticed that my simple implementation
I have a simple html multiline tooltip implementation: this.tooltip = function(tag) { xOffset =
Consider this sample class, class TargetClass { private static String SENSITIVE_DATA = sw0rdfish; private
In this sample code the URL of the app seems to be determined by
Using this sample : I have make my own Fragment that holds tabhost and
A friend show me this sample code to implement HTTP POST in C# and

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.