Just a quick little experiment here to test and demo how you can use the Java iterator object that is accessible from the ColdFusion array object. An array is a collection, and like a lot of collections in Java, it has a method to return an iterator that will iterate over all the items in the collection using the iterator interface.
The one thing that is nice about the iterator is that it uses a very standard, very simple looping syntax. Check it out below:
Launch code in new window » Download code as text file »
To me, this just looks very simple and nice. That is NOT to say that this is better than the traditional index loop from 1 to the ArrayLen() of the array. It's just different.
Download Code Snippet ZIP File
Comments (2) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Array Looping Much Faster Than Query Looping
Creating More ColdFusion Solutions For XStandard Web Services
Yeah, but the catch is that the iterator is easily invalidated. Some quick testing revealed that no matter what you do to the array, any modification to the structure resulted in an invalid iterator and a thrown exception on the next use of the iterator.
I tried deleting elements before and after the iterator with ArrayDeleteAt, as well as inserting elements before and after the iterator with ArrayAppend, etc. All made a big kaboom.
However, the iterator's remove() method still works. I guess that would be useful for stack-type arrays or maybe even a filtering function. The loss of the element index makes the array itself essentially read-or-destroy-only if using an iterator.
Interesting, though.
Posted by Rick O on Sep 9, 2006 at 10:43 AM
Rick,
You are saying you used ArrayDeleteAt() WHILE you were in the iterator? I have not tested that. However, if I manipulate the array before getting the iterator, or after I am done with all iterator features, everything works fine.
If you look at the Java methods for the Iterator, you will see that there is no way to reset the iterator. No MoveToFirst() or anything like that. The iterator creates a one-way reader. You can't use the iterator more than once. If you get an iterator, it doesn't even matter if you manipulate the array - if you want to use the iterator again, you have to re-get it so that it can start from the end.
Posted by Ben Nadel on Sep 10, 2006 at 7:58 AM