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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:49:16+00:00 2026-06-04T20:49:16+00:00

The question might be pretty vague I know. But the reason I ask this

  • 0

The question might be pretty vague I know. But the reason I ask this is because the class must have been made with some thought in mind.

This question came into my mind while browsing through a few questions here on SO.

Consider the following code:

class A
{

    private int myVar;

    A(int varAsArg)
    {
          myVar = varAsArg;
    }

   public static void main(String args[])
   {

      List<A> myList = new LinkedList<A>();
      myList.add(new A(1));
      myList.add(new A(2));
      myList.add(new A(3));

      //I can iterate manually like this:
      for(A obj : myList)
                 System.out.println(obj.myVar);

      //Or I can use an Iterator as well:
      for(Iterator<A> i = myList.iterator(); i.hasNext();)
      {
         A obj = i.next();
         System.out.println(obj.myVar);
      }
   }
}

So as you can see from the above code, I have a substitute for iterating using a for loop, whereas, I could do the same using the Iterator class’ hasNext() and next() method. Similarly there can be an example for the remove() method. And the experienced users had commented on the other answers to use the Iterator class instead of using the for loop to iterate through the List. Why?

What confuses me even more is that the Iterator class has only three methods. And the functionality of those can be achieved with writing a little different code as well.

Some people might argue that the functionality of many classes can be achieved by writing one’s own code instead of using the class made for the purpose. Yes,true. But as I said, Iterator class has only three methods. So why go through the hassle of creating an extra class when the same job can be done with a simple block of code which is not way too complicated to understand either.


EDIT:

While I’m at it, since many of the answers say that I can’t achieve the remove functionality without using Iterator,I would just like to know if the following is wrong, or will it have some undesirable result.

for(A obj : myList)
{
           if(obj.myVar == 1)
                 myList.remove(obj);
}

Doesn’t the above code snippet do the same thing as remove() ?

  • 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-04T20:49:18+00:00Added an answer on June 4, 2026 at 8:49 pm

    First of all, the for-each construct actually uses the Iterator interface under the covers. It does not, however, expose the underlying Iterator instance to user code, so you can’t call methods on it.

    This means that there are some things that require explicit use of the Iterator interface, and cannot be achieved by using a for-each loop.

    Removing the current element is one such use case.

    For other ideas, see the ListIterator interface. It is a bidirectional iterator that supports inserting elements and changing the element under the cursor. None of this can be done with a for-each loop.

    for(A obj : myList)
    {
               if(obj.myVar == 1)
                     myList.remove(obj);
    }
    

    Doesn’t the above code snippet do the same thing as remove() ?

    No, it does not. All standard containers that I know of will throw ConcurrentModificationException when you try to do this. Even if it were allowed to work, it is ambiguous (what if obj appears in the list twice?) and inefficient (for linked lists, it would require linear instead of constant time).

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

Sidebar

Related Questions

I think this might be a pretty simple question, but I haven't been able
This might be a pretty simple question but I'm a Crystal Reports newbie...I have
This question might not seem very technical but, out of curiosity,I want to know
This might be a pretty straightforward question, but I'm trying to understand some of
I know this question might be repeated many times but would really appreciate any
I know this is a pretty basic question, but I don't know how to
This might seem like a pretty detailed question about Easymock, but I'm having a
I might have pretty basic question about regex. I have the following regex, which
might be a stupid question but I seem to be confused. Im pretty new
The question might seem a bit confusing but I have an if statement that

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.