Is there a way to access an iteration-counter in Java's for-each loop?

Yes, you'll have to provide your own counter The reason for this is that the for-each loop internally does not have a counter; it is based on the Iterable interface, i.e. It uses an Iterator to loop through the "collection" - which may not be a collection at all, and may in fact be something not at all based on indexes (such as a linked list).

Yes, you'll have to provide your own counter. The reason for this is that the for-each loop internally does not have a counter; it is based on the Iterable interface, i.e. It uses an Iterator to loop through the "collection" - which may not be a collection at all, and may in fact be something not at all based on indexes (such as a linked list).

I'm afraid this isn't possible with foreach. But I can suggest you a simple old-styled for-loops: List l = new ArrayList(); l. Add("a"); l.

Add("b"); l. Add("c"); l. Add("d"); // the array String array = new Stringl.size(); for(ListIterator it =l.listIterator(); it.hasNext() ;) { arrayit.nextIndex() = it.next(); } Notice that, the List interface gives you access to it.nextIndex().(edit) To your changed example: for(ListIterator it =l.listIterator(); it.hasNext() ;) { int I = it.nextIndex(); doSomethingWith(it.next(), i); }.

You need to run your own counter thus: int I = 0; for(String s : stringArray) { doSomethingWith(s,i); i++; }.

One of the changes Sun is considering for Java7 is to provide access to the inner Iterator in foreach loops. The syntax will be something like this (if this is accepted): for (String str : list : it) { if (str.length() > 100) { it.remove(); } } This is syntactic sugar, but apparently a lot of requests were made for this feature. But until it is approved, you'll have to count the iterations yourself, or use a regular for loop with an Iterator.

Yuval =8-).

There is another way. Given that you write your own Index class and a static method that returns an Iterable over instances of this class you can for (Index each: With. Index(stringArray)) { each.

Value; each. Index; ... } Where the implementation of With. Index is something like class With { public static Iterable index(final T array) { return new Iterable() { public Iterator iterator() { return new Iterator() { index = 0; public boolean hasNext() { return index Size } public Index next() { return new Index(arrayindex, index++); } ... } } } } }.

If you need a counter in an for-each loop you have to count yourself. There is no built in counter as far as I know.

(I've fixed that bug in the question. ) – Tom Hawtin - tackline Jan 25 '09 at 11:39 Check again. There is the same bug in the second code snippet... – EricSchaefer Jan 25 '09 at 11:47.

Looks interesting. I'll have a closer look on that :) – Kosi2801 Mar 27 '10 at 8:21.

There is a "variant" to pax' answer... ;-) int I = -1; for(String s : stringArray) { doSomethingWith(s, ++i); }.

I'm a little surprised no-one suggested the following (I admit it's a lazy approach...); If stringArray is a List of some sort, you could use something like stringArray. IndexOf(S) to return a value for the current count. Note: this assumes that the elements of the List are unique, or that it doesn't matter if they are non-unique (because in that case it will return the index of the first copy found).

There are situations in which that would be sufficient...

1 With a performance approaching O(n²) for the whole loop it's very hard to imagine even a few situations where this would be superior to anything else. Sorry. – Kosi2801 Mar 5 at 21:57.

Yes, you'll have to provide your own counter.

I have two arrays. In first array I have Type and Zone field names. These two field names have multiple values.

I am using smarty.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions