How do you find the sum of all the numbers in a java array

Java 8Object Oriented ProgrammingProgramming

Following program print the sum of the all the numbers in an array.

Example

public class Tester {
   public static void main(String[] args) {
      int[] dataArray = {1, 2, 3, 4};
      int sum = 0;
      for(int i: dataArray) {
         sum += i;
      }
      System.out.println(sum);
   }
}

Output

10
raja
Published on 06-Feb-2018 14:31:47
Advertisements