Community
Participate
Working Groups
I've often written iteration code using the enhanced for-loop initially, then later discovered I needed access to the Iterator to remove an object. It would be nice if Eclipse could do this simple (but verbose) transformation for me. For example: for (final Member member : list) { //... } becomes: for (final Iterator<Member> memberIter = list.iterator(); memberIter.hasNext(); ) { final Member member = memberIter.next(); // ... // if (foo) memberIter.remove(); } Similarly, the transformation could support arrays: for (final Member member : array) { // ... } becomes: for (int memberIndex = 0; memberIndex < array.length; ++memberIndex) { final Member member = array[memberIndex]; // ... // if (foo) member[memberIndex] = null; } Another variation would be creating a ListIterator for lists, to allow adding elements, or doing reverse iteration.
Move to JDT/UI
Suggest to postpone > 3.1
after 3.1
reopen for 3.2
This is also useful if you decide to move back to 1.4 compatibility. Ideally, this decision would be made early on, but if for some reason you have to revert, converting all of the enhanced loops by hand is error prone and tedious.
> Ideally, this decision would be made early on, Sometimes you can't decide, e.g. anybody wishing to use code I have written in Java 1.5 and made public, but needs to use it on Java 1.4 or needs to access the iterator or ... would be very happy to have such a transformation in eclipse.
Setting to helpwanted as we have currently no resources for this feature
Can I just point out a quite common use case for Java 5 code and above: If you have an enhanced for-loop and wish to start using the index for something. I would actually prefer it if the quickfix had 2 options: convert loop to iterator and convert loop to index access (which should only be available if you are looping through arrays or lists).
Created attachment 203086 [details] Implementation and tests
Fixed in HEAD.
"Convert to Iterator for loop" sounds a bit awkward. How about "Convert to Iterator based for loop" or "Use Iterator in for loop" ? "Convert to indexed for loop" can also be changed accordingly to "Use index variable in for loop". Rest looks good.
Verified for 3.8 M2 with I20110912-2126.
My 2 cents... I don't think "indexed" or "Iterator" sound very natural as an adjective. I would recommend: * Convert to for loop having index * Convert to for loop having Iterator
We've already modified the wording a bit with bug 357490 and now use: - Convert to indexed 'for' loop - Convert to Iterator-based 'for' loop The term "indexed" is also used in the JLS and e.g. in http://en.wikipedia.org/wiki/Index_%28computer_science%29 . > * Convert to for loop having index > * Convert to for loop having Iterator I find "having" a bit clumsy. Maybe we could use "with" or "using" instead. Nevertheless, the advantage of the current wording is that the differentiating word comes earlier, so the user has to read less to find the right quick assist e.g. here: public static void main(String[] args) { for (String string : Arrays.asList(args)) { System.out.println(string); } }
(In reply to comment #14) > We've already modified the wording a bit with bug 357490 and now use: > > - Convert to indexed 'for' loop > - Convert to Iterator-based 'for' loop +1 on this wording; the others do seem clumsier. And thanks for implementing this! It's impressive to see that a 6.5 year old feature request still gets noticed.