samedi 30 juillet 2016

Why does the if-block not run for the final case?

Consider the following code, which counts how many of each element an array has:

public static void getCounts(int[] list) {
    int current = list[0];
    int count = 0;
    for (int i = 0; i < list.length; i++, count++) {
        if (list[i] > current) {
            System.out.println(current + " occurs " + count + timeOrTimes(count));
            current = list[i];
            count = 0;
        }
    }
   System.out.println(current + " occurs " + count + timeOrTimes(count));
}

For this question, please assume list is sorted in ascending order. If list is [1, 1, 2, 3, 4, 4], for example, the output is:

1 occurs 2 times
2 occurs 1 time
3 occurs 1 time
4 occurs 2 times

Now, if I get rid of the println that comes after the for-loop, i.e.

public static void getCounts(int[] list) {
    int current = list[0];
    int count = 0;
    for (int i = 0; i < list.length; i++, count++) {
        if (list[i] > current) {
            System.out.println(current + " occurs " + count + timeOrTimes(count));
            current = list[i];
            count = 0;
        }
    }
   // System.out.println(current + " occurs " + count + timeOrTimes(count));
}

Then using the same example input, the output becomes:

1 occurs 2 times
2 occurs 1 time
3 occurs 1 time

In other words, the if block doesn't execute if list[i] is the maximum value of the array. Why is this the case? For the example above, the index of the first 4 is i = 4, and list[4] > 3, so the conditional statement is met, but it still won't execute.

How can I adjust the code so that the if block will run for all cases?

Thanks

Aucun commentaire:

Enregistrer un commentaire