foreach =?ISO-8859-1?Q?doesn=B4t_accept_iterator?=

H

Hendrik Maryns

Hi,

If I have

final private HashMap<FunctionInputTuple, State> transitions = new
HashMap<FunctionInputTuple, State>();

And elsewhere

for (Entry<FunctionInputTuple, State> transition :
transitions.entrySet().iterator()) {

}

Eclipse gives me the error: Can only iterate over an array or an
instance of java.lang.Iterable.
Why does this not work? Or is it a bug in Eclipse?

TIA, H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
 
T

Thomas Fritsch

Hendrik said:
If I have

final private HashMap<FunctionInputTuple, State> transitions = new
HashMap<FunctionInputTuple, State>();

And elsewhere

for (Entry<FunctionInputTuple, State> transition :
transitions.entrySet().iterator()) {

}

Eclipse gives me the error: Can only iterate over an array or an
instance of java.lang.Iterable.
Why does this not work? Or is it a bug in Eclipse?

transitions.entrySet() is already the needed instance of Iterable.
Therefore you have to omit .iterator()
for (Entry<FunctionInputTuple, State> transition :
transitions.entrySet()) { ... }

BTW: I think instead of Entry<...> you have to write Map.Entry<...>
 
T

Thomas Hawtin

It worked in early beta versions of 1.5.0. I believe the rationale for
requiring Iterable and not Iterator, is that it would be less than
obvious that the iterator state is modified. The same goes for passing
iterators to methods. It can be a pain in certain situations.
BTW: I think instead of Entry<...> you have to write Map.Entry<...>

You can import java.util.Map.Entry;, IIRC. My preference is not to
import more than necessary. Just don't import TreeMap.Entry.

Tom Hawtin
 
R

Roedy Green

Eclipse gives me the error: Can only iterate over an array or an
instance of java.lang.Iterable.
An Iterable is something that can produce a Iterator e.g. a
Collection, not the Iterator itself. Sun decided not to support all
the things you might naturally expect for : each to support such as
Iterator, Enumeration, char:String, int:String
 
H

Hendrik Maryns

Thomas Fritsch schreef:
transitions.entrySet() is already the needed instance of Iterable.
Therefore you have to omit .iterator()
for (Entry<FunctionInputTuple, State> transition :
transitions.entrySet()) { ... }

Yes, I realised ust after posting, and cancelled my post, but not fast
enough, it seems. Thanks anyway.
BTW: I think instead of Entry<...> you have to write Map.Entry<...>

Eclipse automatically added the line

import java.util.Map.Entry;

the moment I started about Entries...

H.
--
Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top