298

Is there a way in Java's for-each loop

for(String s : stringArray) {
  doSomethingWith(s);
}

to find out how often the loop has already been processed?

Aside from using the old and well-known for(int i=0; i < boundary; i++) - loop, is the construct

int i = 0;
for(String s : stringArray) {
  doSomethingWith(s);
  i++;
}

the only way to have such a counter available in a for-each loop?

trapzerapzerix
  • 120
  • 3
  • 9
Kosi2801
  • 19,793
  • 12
  • 34
  • 41
  • 2
    Another pity is that you cannot use the loop variable outside the loop, `Type var = null; for (var : set) dosomething; if (var != null) then ...` – Val Sep 05 '13 at 12:45
  • @Val unless the reference is effective final. See my answer for how to use this feature – rmuller Oct 18 '16 at 19:05

15 Answers15

222

No, but you can 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).

interestedparty333
  • 1,980
  • 15
  • 28
Michael Borgwardt
  • 327,225
  • 74
  • 458
  • 699
  • 5
    Ruby has a construct for this and Java should get it too... `for(int idx=0, String s; s : stringArray; ++idx) doSomethingWith(s, idx);` – Nicholas DiPiazza Mar 06 '14 at 22:37
  • 1
    FYI @NicholasDiPiazza there is an `each_with_index` loop method for `Enumerable` in Ruby. See apidock.com/ruby/Enumerable/each_with_index – saywhatnow Dec 04 '15 at 01:40
  • @saywhatnow yeah that's what i meant sorry - not sure what i was smoking when i made the previous comment – Nicholas DiPiazza Dec 04 '15 at 19:30
  • 3
    "The reason for this is that the for-each loop internally does not have a counter". Should be read as "Java developers didn't care to let programmer specify index variable with initial value inside `for` loop", something like `for (int i = 0; String s: stringArray; ++i)` – izogfif Jun 09 '18 at 04:28
71

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<String> each: With.index(stringArray)) {
    each.value;
    each.index;
    ...
}

Where the implementation of With.index is something like

class With {
    public static <T> Iterable<Index<T>> index(final T[] array) {
        return new Iterable<Index<T>>() {
            public Iterator<Index<T>> iterator() {
                return new Iterator<Index<T>>() {
                    index = 0;
                    public boolean hasNext() { return index < array.size }
                    public Index<T> next() { return new Index(array[index], index++); }
                    ...
                }
            }
        }
    }
}
akuhn
  • 25,901
  • 2
  • 72
  • 86
  • 8
    Neat idea. I would have upvoted had it not been for short lived object creation of the Index class. – mR_fr0g Jun 11 '13 at 14:44
  • 5
    @mR_fr0g don't worry, I benchmarked this and creating all these objects is not any slower than reusing the same object for each iteration. The reason is that all these objects are allocated in eden space only and never life long enough to reach the heap. So allocating them is as fast as e.g. allocating local variables. – akuhn Jun 12 '13 at 21:55
  • 1
    @akuhn Wait. Eden space does not mean that no GC is reaquired. Quite the opposite, you need to invoke constructor, scan sooner with GC and finalize. This not only loads CPU but also invalidates the cache all the time. Nothing like this is necessary for local variables. Why do you say that this is "the same"? – Val Sep 01 '13 at 14:03
  • 1
    @Val objects without constructor are allocated by incrementing a pointer. That's all. One instruction. And since none of these objects escapes eden, GC just resets that pointer. At least it used to do that 4 years ago. If you are curious, you can look at the assembly code using the `PrintOptoAssembly` option. – akuhn Sep 02 '13 at 10:37
  • http://oreilly.com/catalog/javapt/chapter/ch04.html says `Reduce the number of temporary objects being used, especially in loops.` It also says `Be aware of the default values that Java initializes Object to null, int to 0, bool to false`. I could not find anything by `PrintOptoAssembly` but I do not think that these initializations can be as simple as pointer increment. Similarly, you need to scan the whole overflown memory to determine the unreferenced objects. I do not believe that GC is as cheap as pointer decrement. – Val Sep 02 '13 at 13:27
  • 1
    @val that book is from 2000, VM technology has improved quite a bit since. – akuhn Sep 03 '13 at 01:53
  • Do you mean that VM improved by not initializing the variables? By not checking which object are referred which are not? How can you improve to become a magician? Is it improved by silencing the issues I raise? – Val Sep 03 '13 at 06:55
  • 1
    @Val you're persistent :) Each thread has a thread local eden space. Our `With` objects are allocated on eden by incrementing a pointer. No initialization needed since the constructor sets all fields. The VM keeps a list of pointers that escaped eden (as far I recall) and our case that list is empty so gc on eden simply resets that pointer. Allocating object used to be expensive in 2000, today it is one of the cheapest operations in a JVM. For example, object pooling used to be a good idea back then but its is very bad idea today. – akuhn Sep 03 '13 at 21:07
  • If you call the constructor to initialize the fields, this does not mean that it is not called (as you stated before) or no time is wasted initializing the fields. You do not even understand that keeping a list of pointers does not tell you whether object is in use or not. Your argument is just spreading the gossip assurance. It contradicts to pure logic and itself. This is not technical and hardly instills confidence. I do not think that java spec has changed since 2000 and logic says that there are aspects that cannot be just optimized. Just confirm that, and I'll back off. – Val Sep 04 '13 at 07:51
  • 7
    If you are worried about performance of short lived objects like this, you are doing it wrong. Performance should be the LAST thing to worry about... readability the first. This is actually a pretty good construct. – Bill K Nov 16 '13 at 08:08
  • 4
    I was going to say I don't really understand the need to debate when the Index instance can be created once and reused/updated, just to moot the argument and make everyone happy. However, in my measurements, the version that creates a new Index() each time performed more than twice as fast on my machine, about equal to a native iterator running the same iterations. – Eric Woodruff Dec 21 '13 at 21:03
56

The easiest solution is to just run your own counter thus:

int i = 0;
for (String s : stringArray) {
    doSomethingWith(s, i);
    i++;
}

The reason for this is because there's no actual guarantee that items in a collection (which that variant of for iterates over) even have an index, or even have a defined order (some collections may change the order when you add or remove elements).

See for example, the following code:

import java.util.*;

public class TestApp {
  public static void AddAndDump(AbstractSet<String> set, String str) {
    System.out.println("Adding [" + str + "]");
    set.add(str);
    int i = 0;
    for(String s : set) {
        System.out.println("   " + i + ": " + s);
        i++;
    }
  }

  public static void main(String[] args) {
    AbstractSet<String> coll = new HashSet<String>();
    AddAndDump(coll, "Hello");
    AddAndDump(coll, "My");
    AddAndDump(coll, "Name");
    AddAndDump(coll, "Is");
    AddAndDump(coll, "Pax");
  }
}

When you run that, you can see something like:

Adding [Hello]
   0: Hello
Adding [My]
   0: Hello
   1: My
Adding [Name]
   0: Hello
   1: My
   2: Name
Adding [Is]
   0: Hello
   1: Is
   2: My
   3: Name
Adding [Pax]
   0: Hello
   1: Pax
   2: Is
   3: My
   4: Name

indicating that, rightly so, order is not considered a salient feature of a set.

There are other ways to do it without a manual counter but it's a fair bit of work for dubious benefit.

paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
28

Using lambdas and functional interfaces in Java 8 makes creating new loop abstractions possible. I can loop over a collection with the index and the collection size:

List<String> strings = Arrays.asList("one", "two","three","four");
forEach(strings, (x, i, n) -> System.out.println("" + (i+1) + "/"+n+": " + x));

Which outputs:

1/4: one
2/4: two
3/4: three
4/4: four

Which I implemented as:

   @FunctionalInterface
   public interface LoopWithIndexAndSizeConsumer<T> {
       void accept(T t, int i, int n);
   }
   public static <T> void forEach(Collection<T> collection,
                                  LoopWithIndexAndSizeConsumer<T> consumer) {
      int index = 0;
      for (T object : collection){
         consumer.accept(object, index++, collection.size());
      }
   }

The possibilities are endless. For example, I create an abstraction that uses a special function just for the first element:

forEachHeadTail(strings, 
                (head) -> System.out.print(head), 
                (tail) -> System.out.print(","+tail));

Which prints a comma separated list correctly:

one,two,three,four

Which I implemented as:

public static <T> void forEachHeadTail(Collection<T> collection, 
                                       Consumer<T> headFunc, 
                                       Consumer<T> tailFunc) {
   int index = 0;
   for (T object : collection){
      if (index++ == 0){
         headFunc.accept(object);
      }
      else{
         tailFunc.accept(object);
      }
   }
}

Libraries will begin to pop up to do these sorts of things, or you can roll your own.

James Scriven
  • 6,719
  • 1
  • 28
  • 35
  • 6
    Not a criticism of the post (it's "the cool way to do it" these days I guess), but I struggle to see how this is easier/better than a simple old school for loop: for (int i = 0; i < list.size(); i++) { } Even grandma could understand this and it's probably easier to type without the syntax and uncommon characters a lambda uses. Don't get me wrong, I love using lambdas for certain things (callback/event handler type of pattern) but I just can't understand the use in cases like this. Great tool, but using it _all the time_, I just can't do. – Manius Feb 08 '18 at 19:51
  • I suppose, some avoidance of off-by-one index over/underflows against the list itself. But there is the option posted above: int i = 0; for (String s : stringArray) { doSomethingWith(s, i); i++; } – Manius Feb 08 '18 at 19:58
24

Java 8 introduced the Iterable#forEach() / Map#forEach() method, which is more efficient for many Collection / Map implementations compared to the "classical" for-each loop. However, also in this case an index is not provided. The trick here is to use AtomicInteger outside the lambda expression. Note: variables used within the lambda expression must be effectively final, that is why we cannot use an ordinary int.

final AtomicInteger indexHolder = new AtomicInteger();
map.forEach((k, v) -> {
    final int index = indexHolder.getAndIncrement();
    // use the index
});
rmuller
  • 10,058
  • 3
  • 50
  • 80
17

I'm afraid this isn't possible with foreach. But I can suggest you a simple old-styled for-loops:

    List<String> l = new ArrayList<String>();

    l.add("a");
    l.add("b");
    l.add("c");
    l.add("d");

    // the array
    String[] array = new String[l.size()];

    for(ListIterator<String> it =l.listIterator(); it.hasNext() ;)
    {
        array[it.nextIndex()] = it.next();
    }

Notice that, the List interface gives you access to it.nextIndex().

(edit)

To your changed example:

    for(ListIterator<String> it =l.listIterator(); it.hasNext() ;)
    {
        int i = it.nextIndex();
        doSomethingWith(it.next(), i);
    }
bruno conde
  • 46,109
  • 14
  • 94
  • 114
10

Idiomatic Solution:

final Set<Double> doubles; // boilerplate
final Iterator<Double> iterator = doubles.iterator();
for (int ordinal = 0; iterator.hasNext(); ordinal++)
{
    System.out.printf("%d:%f",ordinal,iterator.next());
    System.out.println();
}

this is actually the solution that Google suggests in the Guava discussion on why they did not provide a CountingIterator.

Community
  • 1
  • 1
9

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.

Mamun
  • 58,653
  • 9
  • 33
  • 46
Yuval
  • 7,629
  • 11
  • 37
  • 54
5

Though there are soo many other ways mentioned to achieve the same, I will share my way for some unsatisfied users. I am using the Java 8 IntStream feature.

1. Arrays

Object[] obj = {1,2,3,4,5,6,7};
IntStream.range(0, obj.length).forEach(index-> {
    System.out.println("index: " + index);
    System.out.println("value: " + obj[index]);
});

2. List

List<String> strings = new ArrayList<String>();
Collections.addAll(strings,"A","B","C","D");

IntStream.range(0, strings.size()).forEach(index-> {
    System.out.println("index: " + index);
    System.out.println("value: " + strings.get(index));
});
2

For situations where I only need the index occasionally, like in a catch clause, I will sometimes use indexOf.

for(String s : stringArray) {
  try {
    doSomethingWith(s);
  } catch (Exception e) {
    LOGGER.warn("Had some kind of problem with string " +
      stringArray.indexOf(s) + ": " + s, e);
  }
}
Jeremy
  • 1,184
  • 7
  • 15
  • 2
    Be careful that this needs an .equals()-implementation of the Arrays objects which can identify a certain object uniquely. This is *not* the case for Strings, if a certain string is in the Array multiple times you will only get the index of the first occurence, even if you were already iterating on a later item with the same string. – Kosi2801 Jun 16 '17 at 08:55
2

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.

John Topley
  • 107,187
  • 45
  • 188
  • 235
EricSchaefer
  • 22,338
  • 20
  • 63
  • 99
1

There is a "variant" to pax' answer... ;-)

int i = -1;
for(String s : stringArray) {
    doSomethingWith(s, ++i);
}
Jörg
  • 11
  • 1
-1

The best and optimized solution is to do the following thing:

int i=0;

for(Type t: types) {
  ......
  i++;
}

Where Type can be any data type and types is the variable on which you are applying for a loop.

Hari Ram
  • 351
  • 3
  • 17
  • Please provide your answer in a reproducible code format. – Nareman Darwish Jan 04 '20 at 21:19
  • This is exactly the way for which an alternative solution is wanted. The question is not about types but about possibilities to access the loop counter in a DIFFERENT way than counting manually. – Kosi2801 Feb 16 '20 at 18:03
-4

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...

Rik
  • 13
  • 9
    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 05 '11 at 21:57
-5

Here is an example of how I did this. This gets the index at the for each loop. Hope this helps.

public class CheckForEachLoop {

    public static void main(String[] args) {

        String[] months = new String[] { "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST",
                "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" };
        for (String s : months) {
            if (s == months[2]) { // location where you can change
              doSomethingWith(s); // however many times s and months
                                  // doSomethingWith(s) will be completed and 
                                  // added together instead of counter
            }

        }
        System.out.println(s); 


    }
}
ashkanaral
  • 17
  • 7
  • 1
    Sorry, this is not answering the question. It's not about accessing the content but about a counter *how often* the loop already has been iterated. – Kosi2801 Apr 16 '19 at 18:02
  • Question was to find out the current index in the loop in a for-each style loop. – rumman0786 Jul 17 '19 at 05:06
  • Atleast keep it here and rate up as it is a similar answer in my opinion. Thank you for explanations Kosl2801 and rumman0786 – ashkanaral Oct 27 '20 at 00:01