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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:28:55+00:00 2026-06-05T20:28:55+00:00

Iterator<String> iterator=… //right way for (Iterator<String> i = iterator; i.hasNext(); ){ System.out.println(i.next()); } //why

  • 0
Iterator<String> iterator=...

//right way
for (Iterator<String> i = iterator; i.hasNext(); ){
    System.out.println(i.next());
}

//why can't?
for(String i:iterator){         
}

Reference:

http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html

http://docs.oracle.com/javase/6/docs/api/java/util/Iterator.html

  • 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-05T20:28:56+00:00Added an answer on June 5, 2026 at 8:28 pm

    You can do more compactly:

    for(String i:list){
    }  
    

    The syntax is only for Iterables (and arrays), not for their Iterators directly (and also not for Enumerations).

    Why not? I don’t know… Maybe too much complexity/effort (in the compiler implementation) for a “rare” case. Or edge-cases that would cause trouble (such as an Iterable that is also an Iterator, I think some people make such beasts).

    Maybe try libraries like Google Guava to get some more convenient ways to work with Iterators, Collections, and friends.

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

Sidebar

Related Questions

I just came across the following code: for(Iterator<String> iterator = stuff.iterator(); iterator.hasNext();) { ...
I know I can loop through a list of strings like this: list<string>::iterator Iterator;
Given just a std::string iterator, is it possible to determine the start and end
string str = one three; string::iterator it; string add = two ; Lets say
I have this simple code public String toString() { **Iterator it = list.iterator();** String
I'm iterating on an hashtable, concatenating the string values: Iterator<String> it = jsonHashtableTemp.keySet().iterator(); String
For the following two lines of C++ code map<string, vector<size_t> >::iterator beg = mapper.begin();
I have this code: FVDTO.setStatus(fail); List<String[]> invalidFields = new ArrayList<String[]>(); Iterator<ConstraintViolation<HazardSubmission>> iterator = cv.iterator();
does it take constant time to move the iterator to elements of string in
in the following code: int utf8len(char* s, int len) { Glib::ustring::iterator p( string::iterator(s) );

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.