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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:06:21+00:00 2026-06-03T02:06:21+00:00

Several times I have come across cases where the for-each loop would cause problems,

  • 0

Several times I have come across cases where the for-each loop would cause problems, including exceptions and crashes, while the for(it=list.iterator;it.hasNext();) would work without any issues. This includes modifying the collection (which I know shouldn’t happen on for-each, but don’t know why) as well as other cases where I clone stuff. Cant recall any specific example atm I just got thinking about it.

Isn’t the for-each just a shortcut for the second loop type I pointed? Could someone explain exactly whats the difference there?

  • 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-03T02:06:22+00:00Added an answer on June 3, 2026 at 2:06 am

    for-each is just a syntactic sugar introduced in java 1.5. It uses iterator obtained from Iterable behind the scene.

    The only one reasonable difference you mentioned is collection modification during iteration. Yes, it is impossible. Collections cannot be modified during iteration using Iterator. The attempt causes ConcurrentModificationException. This is relevant for both cases (explicit and implicit usage of iterator).

    The only one exception is using Iterator.remove() (when it is supported). In this case iterator does not throw the exception.

    The reason is clear. Iterator cannot iterate collection that is being changed during iteration unless it knows about the change and can re-arrange itself. This is what happens when you are using Iterator.remove().

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

Sidebar

Related Questions

I have come across this problem several times in which I would like to
I have come across this question several times, you need to do some performance
I've come across this several times in the past and have finally decided to
I have a certain loop occurring several times in various functions in my code.
I have a loop MC which will be duplicate to stage several times according
I've come across this several times, ctrl-c randomly does not copy. I think it's
I have several times ran into the same problem, and I would like to
I've come across the same issue several times already. When I display one block
I have seen the following exception case several times public SomeClass(IEnumerable<T> someValues) { if
I have an application (winform exe) that I run several times. Does this mean

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.