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.");
    }
}
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
NoobCoderChick
  • 617
  • 3
  • 21
  • 40

5 Answers5

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);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 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. – NoobCoderChick Jul 03 '15 at 01:49
  • I may use long long because I think int and long may have same size in some platform – ggrr Jul 03 '15 at 02:27
  • @amuse Not in Java, and Java doesn't have a `long long`. See also [JLS-4.2. Primitive Types and Values](http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2). – Elliott Frisch Jul 03 '15 at 02: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=sum*a/b instead of long xxx=sum*a/b. – ggrr Jul 03 '15 at 02: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.");
    }
}
Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87
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.");
    }
}
Jan Siekierski
  • 409
  • 6
  • 14
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 }.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137