Matt Gerrans
Posts: 1153
Nickname: matt
Registered: Feb, 2002
|
|
Re: foreach equivalent in java
|
Posted: May 12, 2003 6:39 PM
|
|
The equivalent in Java is a bit more clunky; get the Iterator from object and iterate over it, like so:
Iterator iter = fields.iterator();
while( iter.hasNext() )
{
MapFieldInfo item = (MapFieldInfo)iter.next();
// ... do your stuff with the item ...
}
If the fields object is implemented in the C# style and works with foreach, then the object implements the IEnumerable interface and has a GetEnumerator() which returns an IEnumerator.
To convert this object to Java, you'd want to have it implement the Collection interface and return an Iterator reference from its iterator() method.
If you are using J#, you might be abled to use the IEnumerable interface of the object directly to get its IEnumerator interface and iterate over it like so:
IEnumerator iter = fields.GetEnumerator();
while( iter.MoveNext() )
{
MapFieldInfo item = (MapFieldInfo)iter.get_Current();
// ... do your stuff with the item ...
}
I'm guessing that the Current property of IEnumerator translates to get_Current() and set_Current() methods in J#; I haven't tried this, though. However, from you what you said, I assume you mean real Java, not J#, so this was only academic.
|
|