Question about ArrayLists and Iterator

As a subjunct to my assignment o' doom (creating an interpreter), right now I'm using a for-each loop to traverse my ArrayList. I've read however that if I want to remove or otherwise modify stuff in my ArrayList I'd better use an (List)Iterator instead. Do I stick with the for-each loop or use a ListIterator? This is particularly important for me because one of the keywords I'm defining (for denoting comments) is supposed to tell the interpreter to ignore everything on that String and go to the next position in the ArrayList. If I was using the for-each loop, how would I do that? Get next index in the ArrayList? Also, I need to maintain a count of how many lines the program's processed, and I'm not sure how to use the for-each loop to do that, or if it's impossible to do that with the enhanced for-loop.

Noam Ingalls wrote:Do I stick with the for-each loop or use a ListIterator?

It depends. When you are looping over a collection such as an ArrayList, and you call one of the remove(...) methods on the ArrayList inside the loop, then the iterator will throw a ConcurrentModificationException the next time the iterator wants to get the next element. The solution is to use an explicit iterator instead of the foreach-loop syntax; you can then call remove() on the iterator (not the ArrayList) to safely remove the element that the iterator is currently pointing to. For example:

If you just want to skip an element (without actually removing it from the list), you can use the foreach-syntax; use the continue keyword to skip to the next iteration of the loop.

If you want to count how often a loop has been iterated and you're using the foreach-syntax, you have to keep a counter yourself:

Jesper de Jong wrote: If you just want to skip an element (without actually removing it from the list), you can use the foreach-syntax; use the continue keyword to skip to the next iteration of the loop.

I've been trying to get this to work with my ArrayList-- I need it to ignore any lines starting with "#". Do I need to split the string?

D'oh! Went through the API and didn't see that for some reason. Thank you Joanne. It works fine now.

On another note: Right now my parser finds the command keyword to map to the HashMap using the indexOf() method. So it's currently set like this:

I have a total of 8 classes (INTEGER, STRING, LET...) that need finding that way. What I'd like to ask is: is there any way to modify that so I can find all those keywords as needed, or do I need to find some other method? They're all at position 0 in the String (keywords always at the beginning of the line).

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742

16

posted Jan 12, 2012 15:49:57

0

The simplest way is a stream of if/else statements

There are other, more OO solutions, which are a bit more complex but ultimately more maintainable. But lets get the program working first. Once you have a working program it's easier to experiment with alternative implementations.