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 9121969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:58:56+00:00 2026-06-17T05:58:56+00:00

I am trying to determine whether the current object within a foreach loop nested

  • 0

I am trying to determine whether the current object within a foreach loop nested inside of another foreach loop, which are both iterating over the same ArrayList, are currently both iterating over the same object. Due to the conventions of the foreach loop, there doesn’t appear to be a simple way to get the current index of the objects in the ArrayList to be able to compare the current objects for equality. Essentially, I would like to continue over the current object within the inner foreach loop if both of the foreach loops are currently iterating over the same object.

For example, given an ArrayList<Object> named objects:

for (Object a : objects) {

    for (Object b : objects) {

        //If both loops are currently iterating over the same object
        //from the ArrayList objects, skip to the next iteration of the inner
        //foreach loop
        if (a == b) {

            continue;

        }

        //If both foreach loops are not iterating over the same object
        //execute some operation

    }

}

Is using the == operator to test whether both objects that are currently being iterated over are the same object reliable, or is it better to simply use regular for loops instead and compare the objects based on their index?

  • 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-17T05:58:57+00:00Added an answer on June 17, 2026 at 5:58 am

    It is OK to compare objects with == in this case, but only if you didn’t put the same object in list multiple times. If you have same object in list on multiple places than this is not OK, and you should use standard for loop.

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

Sidebar

Related Questions

I'm trying to determine whether an object is already contained within a std::set .
I am trying to determine whether a ring is contained by another ring using
I am trying to determine whether a company has websites built with ASP.NET MVC
I'm trying to determine if the current system is 32 or 64 bit. And
I'm trying to determine whether Object1 will collide with Object2 given the following information:
I am trying to determine whether a string input by a user is valid
I'm trying to determine whether or not a string contains a number. This doesn't
C# Question - I'm trying to determine whether it is OK to use a
Am trying to determine whether or not to display an overtime game display flag
I am trying to determine whether I can use a ManualResetEvent here to ensure

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.