Home java How to find the sum of numbers from an array in java?

How to find the sum of numbers from an array in java?

Author

Date

Category

I’m trying to get the sum of all numbers.

For example:

public static void testArray () {
  int myArray [] = {3, 5, 7, 12};
  for (int i = 0; i & lt; myArray.length; i ++) {
    int i2 = i + 1;
    if (i2 & gt; = myArray.length - 1) {
      i2 = 0;
      i2 = 0;
    }
    int sum = myArray [i] + myArray [i2];
    System.out.println (sum);
  }
}

Yes, yes, I know that this code is a piece of nonsense that doesn’t even close to fulfilling its function, but I’ve already broken my head and I’m far from being a mathematician (and not a programmer).

How can I find the sum of the numbers in an array, provided that the length of the array can be arbitrary, and the numbers in it are different, without any sequence?


Answer 1, authority 100%

I honestly did not understand the question. At the beginning it says – I’m trying to get the sum of some numbers … and what does some mean ?? If all numbers, then it is very easy to do:

public static void testArray () {
  int myArray [] = {3, 5, 7, 12};
  int sum = 0;
  for (int i = 0; i & lt; myArray.length; i ++) {
    sum = sum + myArray [i];
  }
  System.out.println (sum);
}

Answer 2, authority 94%

In Java 8 it is possible in 1 line using stream

int myArray [] = {3, 5, 7, 12};
int total = IntStream.of (myArray) .sum ();

similarly, you can use the class Arrays

Arrays.stream (myArray) .sum ()

The stream method has overloads for primitive types and a generic look, which is suitable for arrays of other types.


Answer 3, authority 17%

There are several ways to find the sum of elements in an array:

  • Using Java 8 Stream API (first using the Arrays :: stream convert the array to a stream of integers, then get the sum of this stream using the Stream :: sum )

    int array [] = {3, 5, 7, 12};
    int sum = Arrays.stream (array) .sum ();
    
  • Loop over the elements (for-each loop)

    int array [] = {3, 5, 7, 12};
    int sum = 0;
    for (int element: array)
      sum + = element;
    
  • Loop Using Indexes

    int array [] = {3, 5, 7, 12};
    int sum = 0;
    for (int i = 0; i & lt; array.length; ++ i)
      sum + = array [i];
    

Online code examples:


Answer 4

/ **
 * Sum of all elements from 1 to 1000
 * /
final int sum = Stream.iterate (1, n - & gt; n + 1) .limit (1000) .mapToInt (el - & gt; el) .sum ();

Answer 5

public class test {
  public static int [] myArray;
  public static void main (String [] args) {
    int myArray [] = {3, 5, 7, 12};
    System.out.print (sum (myArray));
  }
  public static int sum (int [] arr) {
    return sum (arr, 0, 0);
  }
  public static int sum (int [] arr, int sum, int count) {
    sum + = arr [count]; 
if (Count & Lt; Arr.Length - 1) Return Sum (Arr, Sum, Count + 1);
    Return Sum;
  }
}

Answer 6

for (int i = 0; i & lt; arraysize; i ++) {
  Numbers [i] = NumberofarrayElements.nextint ();
  SUM = SUM + NUMBERS [I];
}

Answer 7

We consider the amount of two numbers. If you return True :

Public Class Moed2Aquation2a2015 {
  Public Static Void Main (String Args []) {
    INT ARR [] = { -2, 3, 5, 7, 12};
    System.out.PrintLN (SUM2 (Arr, 19));
  }
  Public Static Boolean Sum2 (int [] arr, int num) {
    RETURN SUM2 (ARR, NUM, 0, ARR.LENGTH - 1);
  }
  Public Static Boolean Sum2 (int [] arr, int n, int first, int second) {
    INT Summa = Arr [First] + Arr [Second];
    if (Summa == N) Return True;
    if (second & lt; first & amp; & amp; first == second) Return False;
    if (first & lt; arr.length - 1 & amp; & amp; second! = first)
      Return Sum2 (ARR, N, FIRST, SECOND - 1) || SUM2 (ARR, N, FIRST + 1, SECOND);
    ELSE RETURN FALSE;
  }
}

Answer 8

/ **
 * Calculate Sum of the Array
 * /
Public Static Void Main (String [] Args) {
  INT MYARRAY [] = {3, 5, 7, 12};
  System.Out.print (SUM (MYARRAY));
}
Public Static Int Sum (int [] arr) {
  RETURN SUM (ARR, ARR.LENGTH - 1);
}
Public Static Int Sum (int [] arr, int n) {
  if (n == 0)
    RETURN ARR [0];
  ELSE.
    RETURN ARR [N] + SUM (ARR, N - 1);
}

Answer 9

Here are 3 ways, from the most primitive, to normal. Do not criticize much, I only study.

import java.util.arrays;
// The sum of all elements of the array, the first method
Class BLABLABLA1 {
  Public Static Void Main (String [] Args) {
    int [] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = 0;
    for (int i = 0; i & lt; nums.length; i ++) SUM + = Nums [i];
    System.out.PrintLN ("1st method =" + SUM);
  }
}
// The sum of all elements of the array, the method of the second
Class blablabla2 {
  Public Static Void Main (String [] Args) {
    int [] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = 0;
    SUM = arrays.stream (Nums) .sum ();
    System.out.printLN ("2 way =" + SUM);
  }
}
// The sum of all elements of the array, the method of the third: for-each
Class BLABLABLA3 {
  Public Static Void Main (String [] Args) {
    int [] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int sum = 0;
    For (int x: nums) SUM + = X;
    System.out.printLN ("3rd method =" + SUM);
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions