Jump to content
  • Advertisement
Sign in to follow this  
gamechampionx

Java For Each Question

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In Java, I have something like: Xml.XmlNode myNode; myDoc.Load(readFileName); myDoc.PreserveWhitespace = true; myNodeList = myDoc.DocumentElement.ChildNodes; Now I want to use a For Each type of statement, but I don't know how they are used. I want something along the lines of: for(myNode : myNodeList) to traverse the nodes in the node list. I've "translated" this code from VB, where I have a fully functional program, and this is the only part giving me trouble.

Share this post


Link to post
Share on other sites
Advertisement
Use a for-loop, because as far as I know, Java doesn't have a foreach.


for (int i = 0; i < myNodeList.Count(); ++i)
{
myNode = myNodeList.Get(i);
// Do stuff with myNode
}


Check the documentation of your XML reader on how to get number of nodes and how to obtain a specific node from the list.

Toolmaker

Share this post


Link to post
Share on other sites
Java 1.5 has a foreach. But which XML reader are you using? You need to have an object of type Iterable<E> to do a foreach. (all of the Java collections, Vector, Map, etc, count as Iterables). I remember using the reader at org.w3c.dom, and their NodeList object doesn't support foreach because it doesn't give you access to an Iterable<E>

Anyway if myNodeList is an Iterable, the syntax is:

for (Xml.XmlNode myNode : myNodeList)

and you don't have to declare myNode beforehand

Share this post


Link to post
Share on other sites
I'm using the basic Xml.XmlNodeList data type. I tried using it in the for (...) setup which didn't seem to work, despite working in an identical for each setup in VB.NET . Why would this happen? I suppose I could use the array index method, but I want to sort out this small issue first. Does anybody know why it would work in VB but not in Java using the for each type of setup?

Share this post


Link to post
Share on other sites
Because, as pinacolada said, the class you're using doesn't provide an iterator -- i.e. it doesn't implement Iterable<XmlNodeList>. I'm not at all familiar with that class, so I can't say for sure, but you'll probably have to switch over to a while loop of some sort.

Share this post


Link to post
Share on other sites
Yeah, that's what I figured, but I'm just wanting to get a second opinion from someone who might be familiar with the implementation and style of that particular class.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!