The Artima Developer Community
Sponsored Link

C# Answers Forum
foreach equivalent in java

4 replies on 1 page. Most recent reply: May 13, 2003 1:35 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Uma

Posts: 4
Nickname: uma2003
Registered: Apr, 2003

foreach equivalent in java Posted: May 12, 2003 2:31 AM
Reply to this message Reply
Advertisement
Hi all,

I am doing some conversion process from C# to java. I am struck with c# foreach statement.

Here I give the coding,
foreach (MapFieldInfo item in Fields)
{
if (item == null)
{
continue;
}
if (item.Field.Equals(fieldInfo))
{
exp = item.NewExpression;
break;
}
}

Here the MapFieldInfo in another class. I need the equivalent java for loop for this statement.

Pls could anyone help me.

Thanks in advance.
Uma


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: foreach equivalent in java Posted: May 12, 2003 6:39 PM
Reply to this message Reply
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.

Uma

Posts: 4
Nickname: uma2003
Registered: Apr, 2003

Re: foreach equivalent in java Posted: May 12, 2003 11:09 PM
Reply to this message Reply
Hi matt,

Thanks,for guiding me. It works fine.

What U have said next is true. There is a object which implements the IEnumerator and gets the Enumerator and it uses the current property of Enumerator.

How can I get the enumerator using java? pls guide me.


Thanks
Uma

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: foreach equivalent in java Posted: May 13, 2003 1:34 PM
Reply to this message Reply

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: foreach equivalent in java Posted: May 13, 2003 1:35 PM
Reply to this message Reply
Sounds like you must be using J#, not Java, because I don't see how you could be directly accessing the .Net assemblies with Java. So, try the the code I suggested in the second half of the post above. If you can get and use the IEnumerator "manually" in J# then it is really just longhand for the foreach that C# has, but does exactly the same thing.

As I said, I haven't used J# at all, so I was only guessing how it would work based on the assumption that it has the same syntax as Java (or maybe J# has foreach? I don't know...).

Flat View: This topic has 4 replies on 1 page
Topic: DataSet serialization - WebServices Previous Topic   Next Topic Topic: CD drive with C#

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use