wood burning stoves*
The moose likes Beginning Java and the fly likes Question about ArrayLists and Iterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about ArrayLists and Iterator" Watch "Question about ArrayLists and Iterator" New topic
Author

Question about ArrayLists and Iterator

Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

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.

Thanks for the help!


"How do you define fool?"
"I don't attempt it. I wait for demonstrations. They inevitably surpass my imagination."
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

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?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Noam Ingalls wrote:I need it to ignore any lines starting with "#". Do I need to split the string?

No. Just look at the String javadoc and see if you can find a method that might check what a String starts With.


Joanne
Noam Ingalls
Ranch Hand

Joined: Jan 11, 2012
Posts: 60

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: 3169
    
  10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about ArrayLists and Iterator
 
Similar Threads
Question about cycling through hasmap keys
Iterator or index Which one is preferred.
Iterating arraylist over arrays
How to use logic:iterate strut tag to loop arrayList that holds other arrayList
Which is better?