-1

Can anyone explain this code to me?

class className {
    int[] coeffs;

    int count(int value) {
        int count = 0;
        for (int coeff: coeffs)
            if (coeff == value)
                count++;
        return count;
    }
}

What I really don't understand is this part:

for (int coeff: coeffs)

What is it mean? Thanks for help.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
NoeL
  • 67
  • 1
  • 1
  • 7
  • If you google "Java for loop," you can find the answer in literally the first response. – resueman Jul 19 '13 at 19:04
  • Read [Enhanced For-Loops](https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with) – Smit Jul 19 '13 at 19:05
  • It is a `foreach` loop in java. See http://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work. – chancea Jul 19 '13 at 19:05
  • it as same as this `for(int x=0;x –  Jul 19 '13 at 19:08
  • @user2511414 Not quite. According to the Java Language Specification, it's equivalent to using an Iterator, not an indexed for loop. See: http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.14.2 – Nick Jul 19 '13 at 19:16
  • Wow. I don't realize if it is just an another form of for-loop. Thanks all. – NoeL Jul 19 '13 at 19:19
  • @Nick I see you buddy, I just want to same thing, we all know enough about Iterable. :) –  Jul 19 '13 at 19:22

4 Answers4

1

That is the enhanced for each loop to loop through the array int[] array.

Look at the Oracle documentation

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

This is a foreach loop - it means that for every value in the array coeffs the code inside the for loop will be executed, with the variable coeff representing the value used during that particular iteration.

robjohncox
  • 3,639
  • 3
  • 25
  • 51
1

In earlier versions of Java, there was only the C/Fortran style "for()" loop.

Java 5 (JDK 1.5, 2004) introduced a "foreach()" loop, with the syntax you've described:

http://en.wikipedia.org/wiki/Foreach_loop#Java

for (type item: iterableCollection) {
  // do something to item
}

It's worth noting that although the newer "foreach" syntax might be more "elegant", the "old" for loop index can actually be faster:

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thanks @paulsm4, you explained very complete, and it was very helpful. – NoeL Jul 19 '13 at 19:24
  • The link on performance is antique. These things change on a minor-version basis and this is from two *major* versions ago. – Marko Topolnik Jul 19 '13 at 20:06
  • There, I've just tested it with `jmh`. A loop summing 3200 `Integer`s achieves 1750 runs/second with indexed traversal vs. 1530 runs/second with enhanced for. That's just 14% difference, and if any real work was done in the iteration, it would evaporate completely. – Marko Topolnik Jul 19 '13 at 20:16
0

That's an enhanced for loop. It is structured as follows:

for(Object o: collection)

Basically, objects is an array of objects or primitive, or an Iterable of objects.

Java will iterate over the array or Iterable, set o to the object/value retrieved, and process the block. It allows for quick iteration without dealing with handling your own iterators/

nanofarad
  • 40,330
  • 4
  • 86
  • 117