Using enhanced for statement to total integers in an array. - Java Language Basics

Java examples for Language Basics:for

Description

Using enhanced for statement to total integers in an array.

Demo Code

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);
   } 
}

Result


Related Tutorials