5

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.");
    }
}
4
  • 8
    For future reference, this sort of question would be better suited for codereview.stackexchange.com . Commented Jul 3, 2015 at 1:05
  • 1
    Is this question similar to this one? stackoverflow.com/questions/4550662/… Commented Jul 3, 2015 at 1:06
  • 2
    You should indent your main
    – Quill
    Commented Jul 3, 2015 at 1:07
  • Oh, I thought main was indented. Ok will do, thank you. Commented Jul 3, 2015 at 1:47

5 Answers 5

7

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);
4
  • Honestly I tried something like that but it seemed to be just adding up the indexes so I messed around with it until I got to what I have above. I'll try again, because I just looked at the assignment and it wants an additional loop to print out the elements which cannot happen the way I have it because I changed the array I guess. Commented Jul 3, 2015 at 1:49
  • I may use long long because I think int and long may have same size in some platform
    – ggrr
    Commented Jul 3, 2015 at 2:27
  • @amuse Not in Java, and Java doesn't have a long long. See also JLS-4.2. Primitive Types and Values. Commented Jul 3, 2015 at 2:39
  • oh not pay attention to language (at start I think the answer is language-agnostic), but I doubt if it needs to use long, because I think long likes a trap that may cause overflow if some other programmer do not pay attention to type of sum, for example if I want to use sum to do other calculations I may have something like: int xxx=suma/b instead of long xxx=suma/b.
    – ggrr
    Commented Jul 3, 2015 at 2:58
4
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.");
    }
}
3

shortest way i know would be :

int sum=Arrays.stream(absencesArr).sum();
2

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 }.

Not the answer you're looking for? Browse other questions tagged or ask your own question.