问题
I'm working on a simple assignment for a summer java course and was just hoping you guys could take a look at my code and see if the way I did it is the best way. The purpose is to create a simple int
array with at least 25 elements and use a loop to traverse it and add up all the elements. I had some issues but looks like I got it to work. After I work it out I did a little research and saw some similar stuff where people were using a For Each loop (enhanced loop). Would that be a better option? I'm kinda confused on the best ways to use that opposed to a regular for loop.
Anyway, any comments or criticism in helping me be 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.");
}
}
回答1:
Don't modify the array. I would prefer the for-each loop. And you should consider that there may be a very large number of students, so I would probably use a long
for sum
. And formatted output. Putting that together 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);
回答2:
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.");
}
}
回答3:
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.");
}
}
回答4:
shortest way i know would be :
int sum=Arrays.stream(absencesArr).sum();
回答5:
In addition to other nice contributions, I am fan of for-each loop
and will typically do it in 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 have control over my Object size/length/count, I will use for loop
like 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 then one line of codes inside the for loop
or for-each loop
then I will put them all inside curly brackets { more than one line of code }
.
来源:https://stackoverflow.com/questions/31196967/sum-of-elements-in-an-array