docs:programming:java:foreach_loops

foreach loops

:!: You don't actually use foreach in java, but rather use: “for(<type> item : items)”

The following program uses the enhanced for to loop through the array:

    class EnhancedForDemo {
         public static void main(String[] args){
              int[] numbers = {1,2,3,4,5,6,7,8,9,10};
              for (int item : numbers) {
                System.out.println("Count is: " + item);
              }
         }
    }

In this example, the variable item holds the current value from the numbers array. The output from this program is the same as before:

    Count is: 1
    Count is: 2
    Count is: 3
    Count is: 4
    Count is: 5
    Count is: 6
    Count is: 7
    Count is: 8
    Count is: 9
    Count is: 10
  • docs/programming/java/foreach_loops.txt
  • Last modified: 2008/08/03 00:25
  • by 127.0.0.1