Sum of elements in an array

I am working on a simple assignment for a java summer course and just hoping you guys can take a look at my code and see the way I did it is the best way. The goal is to create a simple int array with at least 25 elements and use a loop to move it and add all the elements. I had some problems, but it looks like I got him to work. After I developed this, I did a little work and saw similar things in which people used the For Each loop (reinforced loop). Would this be the best option? I am a bit confused about the best use cases that contradict the regular cycle.

In any case, any comments or criticisms that help me become a better programmer!

 public class Traversals { public static void main(String[] args) { int absenceTotal = 0; // initialize array with 30 days of absences. int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1, 11, 23, 5, 6, 7, 10, 1, 5, 14, 2, 4, 0, 0, 1, 3, 2, 1, 1, 0, 0, 1, 3, 7, 2 }; for (int i = 0; i < absencesArr.length; i++) { absencesArr[i] += absenceTotal; absenceTotal = absencesArr[i]; } System.out.println("There were " + absenceTotal + " absences that day."); } } 
+5
source share
4 answers

Do not modify the array. I would prefer a for-each loop. And you have to consider that there can be a lot of students, so I would probably use long for sum . And formatted output. Combining this into something like

 long sum = 0; for(int i : absencesArr) { sum += i; } // System.out.println("There were " + sum + " absences that day."); System.out.printf("There were %d absences that day.%n", sum); 
+7
source
 public class Traversals { public static void main(String[] args) { int absenceTotal = 0; // initialize array with 30 days of absences. int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1, 11, 23, 5, 6, 7, 10, 1, 5, 14, 2, 4, 0, 0, 1, 3, 2, 1, 1, 0, 0, 1, 3, 7, 2 }; for (int i = 0; i < absencesArr.length; i++) { // remove this //absencesArr[i] += absenceTotal; absenceTotal += absencesArr[i]; //add this } System.out.println("There were " + absenceTotal + " absences that day."); } } 
+4
source

In addition to other nice contributions, I am a fan of for-each loop and usually do it on one line.

 for(int i : absencesArr) absenceTotal += i; System.out.printf("There were %d absences that day.", absenceTotal); 

But in some situations, when I want to control my object size / length / counter, I will use for loop , as in the following example:

 for (int i = 0; i < absencesArr.length; i++) absenceTotal += absencesArr[i]; System.out.printf("There were %d absences that day.", absenceTotal); 

And if I need to have more than one line of codes inside a for loop or for-each loop , then I put them all in curly braces { more than one line of code } .

+2
source

In Java 8, you can use stream api:

 public class Traversals { public static void main(String[] args) { int absenceTotal = 0; // initialize array with 30 days of absences. int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1, 11, 23, 5, 6, 7, 10, 1, 5, 14, 2, 4, 0, 0, 1, 3, 2, 1, 1, 0, 0, 1, 3, 7, 2 }; absenceTotal = IntStream.of(array).sum(); System.out.println("There were " + absenceTotal + " absences that day."); } } 
+1
source

All Articles