Java examples for Language Basics:for
Using enhanced for statement to total integers in an array.
public class Main { public static void main(String[] args) {//w w w . j a va 2s.com int[] array = {87, 68, 94, 10, 83, 78, 851, 91, 76, 87}; int total = 0; // add each element's value to total for (int number : array) total += number; System.out.printf("Total of array elements: %d%n", total); } }